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

NEW_DFS = {
    "dlg_show_title": {
        "di": "060098",
        "da": "044020",
        "df_fieldnumber": 753054,
        "position": 130,
        "created_on": "02.07.14",
        "modified_on": "02.07.14",
        "rwdu": 7,
        "created_by": "MIGRATION",
        "modified_by": "MIGRATION",
        "own_lic": "100",
        "opc": None,
        "owner": 7,
        "group": 7,
        "any": 7,
        "window": 9,
        "behavior": "o",
        "default_value": "@Y"},

    "dlg_movable": {
        "di": "060069",
        "da": "044020",
        "df_fieldnumber": 753055,
        "position": 131,
        "created_on": "02.07.14",
        "modified_on": "02.07.14",
        "rwdu": 7,
        "created_by": "MIGRATION",
        "modified_by": "MIGRATION",
        "own_lic": "100",
        "opc": None,
        "owner": 7,
        "group": 7,
        "any": 7,
        "window": 9,
        "behavior": "o",
        "default_value": "@Y"},

    "dlg_closable": {
        "di": "060067",
        "da": "044020",
        "df_fieldnumber": 753056,
        "position": 132,
        "created_on": "02.07.14",
        "modified_on": "02.07.14",
        "rwdu": 7,
        "created_by": "MIGRATION",
        "modified_by": "MIGRATION",
        "own_lic": "100",
        "opc": None,
        "owner": 7,
        "group": 7,
        "any": 7,
        "window": 9,
        "behavior": "o",
        "default_value": "@Y"},

    "dlg_closebtn": {
        "di": "062203",
        "da": "044020",
        "df_fieldnumber": 753057,
        "position": 133,
        "created_on": "02.07.14",
        "modified_on": "02.07.14",
        "rwdu": 7,
        "created_by": "MIGRATION",
        "modified_by": "MIGRATION",
        "own_lic": "100",
        "opc": None,
        "owner": 7,
        "group": 7,
        "any": 7,
        "window": 9,
        "behavior": "o",
        "default_value": "@Y"},

    "dlg_pos": {
        "di": "060068",
        "da": "044020",
        "df_fieldnumber": 735406,
        "position": 126,
        "created_on": "13.06.12",
        "modified_on": "27.05.13",
        "rwdu": 7,
        "created_by": "MIGRATION",
        "modified_by": "MIGRATION",
        "own_lic": "100",
        "opc": None,
        "owner": 7,
        "group": 7,
        "any": 7,
        "window": 9,
        "behavior": "o",
        "default_value": "5"}}


class SetAdditionalDefaultValues(ChangeDTPPacket):
    """sets some additional dafaultvalues for dialogfields in modules"""
    version = 1
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 10955

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

    def fix(self, runner):
        succeed = 0

        for datafield in NEW_DFS:
            da_dict = NEW_DFS.get(datafield)
            current_datafield_record = self.get_record(
                411, [da_dict.get("da"), da_dict.get("df_fieldnumber")], [213, 223])
            if current_datafield_record is None:
                current_datafield_record = ppms.create_record(411, da_dict)
                if current_datafield_record is not None:
                    if current_datafield_record.save():
                        succeed += 1
            else:
                if current_datafield_record.di.get_value() != da_dict.get("di"):
                    self.log(
                        "FAILED: There is already an other datafield with id '{df_id}' in dataarea '{da_id}' existent."
                        .format(df_id=current_datafield_record.df_fieldnumber.get_value(), 
                                da_id=current_datafield_record.da.get_value()))
                else:
                    if self.change_value(current_datafield_record, "default_value", None, da_dict.get("default_value")):
                        succeed += 1

        if succeed < len(NEW_DFS.keys()):
            return self.fail()
        return self.success()