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


class CreateDialogueColumns(SQLPacket):
    """Create new columns for width and height of dialogue modules."""
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18833

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

    def columns_already_exist(self):
        return self.column_exists(table_name='DT405', column_name='HEIGHT_DIALOGUE') and self.column_exists(
            table_name='DT405', column_name='WIDTH_DIALOGUE')

    def fix(self, packet_runner):
        if self.columns_already_exist():
            self.log('Columns already exist')
            return self.success()

        add_dis = {DBMS_ORACLE: ["""ALTER TABLE DT405
                                  ADD HEIGHT_DIALOGUE NUMBER(5, 0) DEFAULT 0""",
                                 """ALTER TABLE DT405
                                    ADD WIDTH_DIALOGUE NUMBER(5, 0) DEFAULT 0""",
                                 """ALTER TABLE DT405_HIS
                                   ADD HEIGHT_DIALOGUE NUMBER(5, 0) DEFAULT 0""",
                                 """ALTER TABLE DT405_HIS
                                   ADD WIDTH_DIALOGUE NUMBER(5, 0) DEFAULT 0"""],
                   DBMS_MSSQL: ["""ALTER TABLE DT405
                                  ADD HEIGHT_DIALOGUE NUMERIC(5, 0)
                                  DEFAULT 0""",
                                """UPDATE DT405
                                    SET HEIGHT_DIALOGUE = DEFAULT""",
                                """ALTER TABLE DT405
                                  ADD WIDTH_DIALOGUE NUMERIC(5, 0)
                                  NOT NULL DEFAULT 0""",
                                """UPDATE DT405
                                    SET WIDTH_DIALOGUE = DEFAULT""",
                                """ALTER TABLE DT405_HIS
                                    ADD HEIGHT_DIALOGUE NUMERIC(5, 0)
                                    NOT NULL DEFAULT 0""",
                                """UPDATE DT405_HIS
                                    SET HEIGHT_DIALOGUE = DEFAULT""",
                                """ALTER TABLE DT405_HIS
                                  ADD WIDTH_DIALOGUE NUMERIC(5, 0)
                                  NOT NULL DEFAULT 0""",
                                """UPDATE DT405_HIS
                                    SET HEIGHT_DIALOGUE = DEFAULT"""]}
        for sql_statement in add_dis[self.DBMS]:
            self.modify_and_log(sql_statement)

        return self.success()
