from ppms.migration import ChangeDTPPacket, SQLPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType

DATAFIELD_DICTIONARY = {
                        
                        ('054395', 715234): {'window': [9, 1], 'width': [0,254], 'symbol_id': ['','001968']},
                        ('054392', 715266): {'window': [9, 1], 'width': [0,254], 'symbol_id': ['','001968']},
                        
                        ('054395', 715225): {'width': [656, 518]},
                        ('054395', 715226): {'width': [759, 571]},
                        ('054395', 715227): {'width': [210, 197]},
                        ('054395', 715228): {'width': [260, 241]},
                        ('054395', 715229): {'width': [150, 293]},
                        ('054395', 715230): {'width': [300, 260]},
                        
                        ('054392', 715257): {'width': [656, 518]},
                        ('054392', 715258): {'width': [759, 571], 'height': [0, 40]},
                        ('054392', 715259): {'width': [210, 197]},
                        ('054392', 715260): {'width': [260, 241]},
                        ('054392', 715261): {'width': [150, 293]},
                        ('054392', 715262): {'width': [300, 260]}
                    }


class ReportColumnEingetretenIsMissing(ChangeDTPPacket, SQLPacket):
    """changes window-value and fixes customizing for risk and chance-dataareas in report"""
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10955
    sql_file = "WI12552_Report_column_Eingetreten_is_missing.sql"
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.5', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.5', version_type=VersionType.CUSTOMIZING))
                
    def change_record(self, datatable, primary_key, change_dictionary):
        old_value = 0
        new_value = 1
        record = self.get_record(datatable, list(primary_key), list(change_dictionary.keys()))
        for field in change_dictionary.keys():
            old_new_values = change_dictionary.get(field)
            self.change_value(record, field, old_new_values[old_value], old_new_values[new_value])
            
    def fix(self, runner):
        ## change customizing of datafields
        for datafield in DATAFIELD_DICTIONARY.keys():
            self.change_record(411, datafield, DATAFIELD_DICTIONARY.get(datafield))
            
        ## create itexts for df-heading
        self.apply_dml_file(self.sql_planta_de_dbms, self.sql_file)
            
        ## set itexts for df-heading
        self.modify_and_log("update dt411 set di010200 = 801130 where di000306 = '054395' and di000307 = 715234")
        self.modify_and_log("update dt411 set di010200 = 801131 where di000306 = '054392' and di000307 = 715266")
        
        return self.success()