# -*- coding: utf-8 -*-

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


class A_EnhanceAggregationTypeValueRange(ChangeDTPPacket):
    """PF check formula on DI 000248 "Aggregation Type" used to allow values in the range of [0..4], enhance to [0..5].
    """

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

    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):
        di000248 = self.get_record(412, ['000248'], ['valuerange'])
        if self.change_value(di000248, 'valuerange', '0,1,2,3,4', '0,1,2,3,4,5'):
            return self.success()
        else:
            return self.fail('''Unable to change DI 000248's formula!''')


class B_EnhanceAggregationTypeListbox(SQLPacket):
    """Add aggregation type "Never aggregate, always calculate" to the listbox used in the Data Area customizer module.

    Also fixes some english translations in the aggregation context (summarization -> aggregation)
    """

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

    FILENAME = 'wi13908_new_aggregation_type.sql'
    __PATH = r'migration.hotfix.S_39_5_14.wi13908_add_aggregation_type_never_to_customizing.A_EnhanceAggregationTypeValueRange'
    UPDATE = ("""update dt435 set di001549 = 'Aggregation type' where di001538 = 98 and di001550 = 'EN' and di001549 = 'Summarization type'""",
              """update dt435 set di001549 = 'Aggregation type' where di001538 = 1567 and di001550 = 'EN' and di001549 = 'Summarization type'""",
              """update dt435 set di001549 = 'Invalid aggregation type' where di001538 = 6411 and di001550 = 'EN' and di001549 = 'Invalid summarization type'""",
              """update dt435 set di001549 = 'Aggregation type' where di001538 = 36167 and di001550 = 'EN' and di001549 = 'Summarization type'""",
              """update dt435 set di001549 = 'No aggregation (for DFs "Aggr. type" from DI)' where di001538 = 36168 and di001550 = 'EN' and di001549 = 'No summarization (for DFs "Summ.type" from DI)'""",
              """update dt435 set di001549 = 'Sum' where di001538 = 36171 and di001550 = 'EN' and di001549 = 'Total'""",
              """update dt435 set di001549 = 'Start aggregation?' where di001538 = 89681 and di001550 = 'EN' and di001549 = 'Start summarization?'""",
              """update dt435 set di001549 = 'Incorrect aggregation type on DF %s in the data area %s' where di001538 = 741627 and di001550 = 'EN' and di001549 = 'Incorrect summarization type on DF %s in the data area %s'""")

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4', version_type=VersionType.CUSTOMIZING),
                MaxVersionDependency(version='39.5.5', version_type=VersionType.CUSTOMIZING),
                PacketDependency(packet_path=self.__PATH, expected_state=PacketState.INSTALLED))

    def fix(self, runner):
        for statement in self.UPDATE:
            self.modify_and_log(statement)
        self.apply_dml_file(self.sql_planta_de_dbms, self.FILENAME)
        return self.success()
