# -*- coding: utf-8 -*-
from ppms.migration import ChangeDTPPacket, PacketCategory, MinVersionDependency, MaxVersionDependency, VersionType
from ppms import ppms
from ppms.constants import SYS_VAR_LICENSE

class HideProjectSeparator(ChangeDTPPacket):
    '''Hide dataitem DI000256 "Separator" in Module 006355

    An update for the module 006355 to set di000256 to window 9 
    and df-option to 0 
    '''
    wi_number = 10668  
    version = 1
    done_after_success = True  
    run_on_startup = True  
    category = PacketCategory.CATEGORY_MANDATORY  
    
    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.5', version_type=VersionType.CUSTOMIZING))        

    def fix(self, runner):
        # Hide dataitem "separator" in module 006355
        self.log('Hide dataitem "separator" in module 006355')
        
        # variables
        errors = 0
        df_key = []
        df_columns = ['window', 'sc', 'option']
        df_dict = {'window': 9, 'sc': 0, 'option': 0}
        db_record = ppms.search_record(345, [ppms.uvar_get(SYS_VAR_LICENSE)], ['data_base_changeset'])
        db_version = ""
        if db_record:
            db_version = db_record.data_base_changeset.get_value()

        # determine datafield by version
        if db_version.startswith('39.5'):
            df_key = ["025822", 742593]
        elif db_version.startswith('39.4'):
            df_key = ["025822", 137856]
        else:
            return self.fail('Encountered errors')
        
        # edit datafield
        for py_name in df_columns:
            datafield_dtp = self.get_record(411, df_key, df_columns)
            check_value = self.change_value(datafield_dtp, py_name, 1, df_dict[py_name])
            if not check_value:
                errors += 1
        
        if errors:
           return self.fail('Encountered errors')

        return self.success()
        