#!/usr/bin/python

import json
from edge.path_helpers import *

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


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

pipelines_to_migrate = filter(lambda x: x['type'] == PTYPE_LPR_MV or x['type'] == PTYPE_SPEED, pipeline_jnode_set)

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

for pipeline_id in migrate_list:

    ui_jnode_filepath = get_ui_pipeline_jnode_filepath(pipeline_id, UI_TYPE_VIDEOSOURCE, '0')
    with open(ui_jnode_filepath, 'r') as json_file:
        ui_jnode = json.load(json_file)

    videostream_control_jnode = ui_jnode['rw']['videostream']['control']

    value = videostream_control_jnode.get('gain-auto-min')
    if value is not None:
        videostream_control_jnode['gain-auto-min'] = int(value)

    value = videostream_control_jnode.get('gain-auto-max')
    if value is not None:
        videostream_control_jnode['gain-auto-max'] = int(value)

    value = videostream_control_jnode.get('gain-manual')
    if value is not None:
        videostream_control_jnode['gain-manual'] = int(value)

    ui_jnode['rw']['videostream']['control'] = videostream_control_jnode

    with open(ui_jnode_filepath, 'w') as json_file:
        json.dump(ui_jnode, json_file, indent=2)

