#!/usr/bin/python
from edge.com import fs
from edge import path_helpers as ph
import ujson


def _get_footer_jnode(max_w):
    """
    :type max_w: float
    :rtype: dict
    """
    jnode = {
        "font-height": 14,
        "rows": [{
            "enabled": True,
            "cols": [
                {
                    "width": max_w / 3,
                    "align": "center",
                    "format": "${datetime-ms}"
                },
                {
                    "width": max_w / 3,
                    "align": "center",
                    "format": "${geolocation}"
                },
                {
                    "width": max_w / 3,
                    "align": "center",
                    "format": "${serial}"
                }]
        }]
    }
    return jnode


_PIPELINE_SET_FILEPATH = '/etc/edge/setupstore/admin/actions/pipeline.json'


with open(_PIPELINE_SET_FILEPATH, 'r') as json_file:
    pipeline_jnode_set = ujson.load(json_file)


migrate_list = map(lambda x: (x['id'], x['type']), pipeline_jnode_set)

for pipeline_id, pipeline_type in migrate_list:
    ui_http_jnode_filepath = ph.get_ui_pipeline_jnode_filepath(pipeline_id, ph.UI_TYPE_HTTP_STREAMING, '0')

    http_schemapath = ph.get_ui_pipeline_schema_filepath(pipeline_id, ph.UI_TYPE_HTTP_STREAMING, '0')
    fs.ln_schema(http_schemapath, ui_http_jnode_filepath)

    if pipeline_type in [ph.PTYPE_SPEED, ph.PTYPE_LPR_MV]:
        ui_rtsp_jnode_filepath = ph.get_ui_pipeline_jnode_filepath(pipeline_id, ph.UI_TYPE_RTSP_STREAMING, '0')
        rtsp_schemapath = ph.get_ui_pipeline_schema_filepath(pipeline_id, ph.UI_TYPE_RTSP_STREAMING, '0')
        fs.ln_schema(rtsp_schemapath, ui_rtsp_jnode_filepath)

        ui_rtsp_jnode = fs.load_json2(ui_rtsp_jnode_filepath)
        if ui_rtsp_jnode['rw']['live'].get('footer') is None:
            ui_identity = ph.UiIdentity(pipeline_type, ph.UI_TYPE_RTSP_STREAMING, '0')
            image_w = ui_rtsp_jnode['rw']['live']['videostream']['resolution']['max-width']

            # add frame-w
            ui_rtsp_jnode['rw']['live']['videostream']['frame-w'] = image_w

            # add footer args
            ui_rtsp_jnode['rw']['live']['footer'] = _get_footer_jnode(image_w)
            fs.write_json_file(ui_rtsp_jnode, ui_rtsp_jnode_filepath, pretty=True)

    ui_http_jnode = fs.load_json2(ui_http_jnode_filepath)
    if ui_http_jnode['rw']['live']['renderer']['vehicle'].get('video-plate-center') is None:
        ui_http_jnode['rw']['live']['renderer']['vehicle']['video-plate-center'] = 'never'
        fs.write_json_file(ui_http_jnode, ui_http_jnode_filepath, pretty=True)

    if ui_http_jnode['rw']['live'].get('footer') is None:
        ui_identity = ph.UiIdentity(pipeline_type, ph.UI_TYPE_HTTP_STREAMING, '0')
        image_w = ui_http_jnode['rw']['live']['videostream']['resolution']['max-width']

        # add frame-w
        ui_http_jnode['rw']['live']['videostream']['frame-w'] = image_w

        # add footer args
        ui_http_jnode['rw']['live']['footer'] = _get_footer_jnode(image_w)
        fs.write_json_file(ui_http_jnode, ui_http_jnode_filepath, pretty=True)
