# -*- coding: utf-8 -*-
import os

from ppms import ppms
from ppms.migration.packet import *


class AddTrafficLightForDeactivatedDIs(SQLPacket):
    '''Creates a traffic light dataitem for inactive datafields.

    Two new Dataitems will be created
    The first shows the "Enabled" value of the original dataitem
    the second is a traffic light dataitem which turns Text red,
    if the selected dataitem is not enabled.
    '''

    wi_number = 9998
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY

    sql_file = 'WI9998_Traffic_Light_DI_for_CU.sql'

    def fix(self, runner):
        di_not_exist = True 
        check_query = "select di000238 from dt412 where di000238 in ('062619', '062620')"
        check_list = ppms.db_select(check_query)
        for rec in check_list:
            if rec:
                di_not_exist = False

        if di_not_exist:
            sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, self.sql_file)
            queries = self.get_queries_from_file(sqlpath)
        
            for query in queries:
                self.modify_and_log(query)
            self.log('dataitems created successfully')
        else:
            self.log('dataitems already exists')

        return self.success()