# -*- coding: utf-8 -*-
from ppms.migration import *
import hql


class AddAndChangeDisLongestPathAndBucketId(ChangeDTPPacket, SQLPacket):
    """Changes the DI longest_critical_path (DT463 and DT824) to an number filed, add longest_critical_path to aob,
    add bucketID to project.
    longest_critical_path to match the bitfield calculated by rouge scheduler. The new Values are:
    0 = No critical Path
    1 = on Projects Critical Path
    2 = on Buckets Critical Path
    3 = on Projects and Buckets Critical Path"""
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 21970

    filename_aob_ddl = 'WI21970_create_aob_longest_path_di_ddl.sql'
    filename_aob_del = 'WI21970_create_aob_longest_path_di_del.sql'
    filename_aob_dml = 'WI21970_create_aob_longest_path_di.sql'

    filename_bucket_ddl = 'WI21970_create_project_bucket_di_ddl.sql'
    filename_bucket_del = 'WI21970_create_project_bucket_di_del.sql'
    filename_bucket_dml = 'WI21970_create_project_bucket_di.sql'

    def fix(self, packet_runner):
        self.log_heading("Dropping/creating necessary column(s)")
        self.drop_column('DT461', 'BUCKET_ID')
        self.drop_column('DT823', 'BUCKET_ID')
        self.drop_column('DT465', 'LONGEST_PATH')
        self.drop_column('DT825', 'LONGEST_PATH')

        self.change_type_of_longest_critical_path()
        self.create_aob_critical_path_di()
        self.create_project_bucket_dis()
        return self.success()

    def change_type_of_longest_critical_path(self):
        record = self.get_record(dt_num=412, key_list=['064921'],
                                 di_list=['column_type', 'df_type', 'ppms_type', 'df_length', 'db_length'])
        if not (self.change_value(record=record, attribute='df_type', old_value = None, new_value='N')
                and self.change_value(record=record, attribute='column_type', old_value = None, new_value=20)
                and self.change_value(record=record, attribute='ppms_type', old_value = None, new_value='N2')
                and self.change_value(record=record, attribute='df_length', old_value = None, new_value=2)
                and self.verify_value(record=record, attribute='db_length', value=2)):
            return self.fail(
                'Could not change the type of DI064921 (DT463), maybe there was some individual customizing')

        record = self.get_record(dt_num=412, key_list=['064924'],
                                 di_list=['column_type', 'df_type', 'ppms_type', 'df_length', 'db_length'])
        if not (self.change_value(record=record, attribute='df_type', old_value = None, new_value='N')
                and self.change_value(record=record, attribute='column_type', old_value = None, new_value=20)
                and self.change_value(record=record, attribute='ppms_type', old_value = None, new_value='N2')
                and self.change_value(record=record, attribute='df_length', old_value = None, new_value=2)
                and self.verify_value(record=record, attribute='db_length', value=2)):
            return self.fail(
                'Could not change the type of DI064924 (DT824), maybe there was some individual customizing')

    def create_aob_critical_path_di(self):
        self.log_heading("Deleting/Inserting new data for longest paths (AOB)")
        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.filename_aob_ddl, split=True, separator=';/')
        self.apply_dml_file(self.sql_planta_de_ansi, self.filename_aob_del)
        self.apply_dml_file(self.sql_planta_de_dbms, self.filename_aob_dml)

    def create_project_bucket_dis(self):
        self.log_heading("Deleting/Inserting new bucket IDs")
        self.apply_fix_ddl_file(self.sql_planta_de_dbms, self.filename_bucket_ddl, split=True, separator=';/')
        self.apply_dml_file(self.sql_planta_de_ansi, self.filename_bucket_del)
        self.apply_dml_file(self.sql_planta_de_dbms, self.filename_bucket_dml)

