# -*- coding: utf-8 -*-
import os
from ppms import ppms
from ppms.constants import *
from ppms.migration.packet import *
from ppms.migration import SQLPacket, PacketCategory, VersionType, MinVersionDependency, MaxVersionDependency


DATA_TABLE_DF = ppms.db_select("SELECT DI000307, DI001992 FROM DT411 where DI000306 = '031149' ORDER BY DI001992")
DATA_ITEM_DF =  ppms.db_select("SELECT DI000307, DI001992 FROM DT411 where DI000306 = '031148' ORDER BY DI001992")
DT_AUDIT_POS = 8
DI_AUDIT_POS = 23


def execute_sql(self,file):
        sqlpath = self.get_sql_file_path(self.sql_planta_de_dbms, file)

        queries = self.get_queries_from_file(sqlpath)

        for query in queries:
            try:
                self.modify_and_log(query)
            except Exception as e:
                if 'ORA-00001' in str(e):
                    self.log("Skipped because change has already been made")
                else:
                    raise e


class B_AuditVenusCustomizing(SQLPacket, ChangeDTPPacket):
    '''Customizing DataDictionary for auditing- add DataFields'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12638
    packet_deps_path = 'migration.server.S_39_5_11.wi12638_audited_venus.AuditVenusDataItem'

    fname = 'WI12638_add_audited_customizing_venus.sql'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5'),
                MaxVersionDependency(version='39.5.5'))

    def fix(self, runner):
        success = True
        if not self.is_position_exist(DATA_TABLE_DF,DT_AUDIT_POS):
            success = self.move_datafields(DATA_TABLE_DF,DT_AUDIT_POS,'031149')

        if not self.is_position_exist(DATA_ITEM_DF,DI_AUDIT_POS):
            success = self.move_datafields(DATA_TABLE_DF,DT_AUDIT_POS,'031148')

        if success:
            execute_sql(self,self.fname)
        return self.success()

    def move_datafields(self, list,audit_position,dataarea):
        errors = 0
        index = audit_position
        for li in list:
            df_id = li[0]
            position = li[1]
            if index == position:
                dtp_rec = self.get_record(411, [dataarea, df_id], [1992])
                if not self.change_value(dtp_rec, 'position', position, position + 1):
                    errors += 1
                index += 1
                if not self.is_position_exist(list,position + 1):
                    break;

        if not errors:
            return True
        else:
            return False

    def is_position_exist(self, list,position):
        for li in list:
            if position in li:
                return True
        return False


class A_AuditVenusDataItem(SQLPacket, ChangeDTPPacket):
    '''Add DataItem for auditing in DT415 and in DT412'''
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 12638

    fname = 'WI12638_add_audited_data_items_venus.sql'

    def get_dependencies(self):
        return (MinVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.5', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        execute_sql(self,self.fname)
        return self.success()
