# -*- coding: utf-8 -*-
from ppms.migration import *


CODE = '''def computeOutput(di):
    dependend_dfs = {"dlg_closebtn",
                     "dlg_movable"
                     }
    # if dlg_show_title is true, dlg_closebtn and dlg_movable will be available for the user
    # if dlg_show_title is false, they won't be available (output)

    for di_name in dependend_dfs:
        if di.get_value():
            dtp_rec = di.get_dtp_record()
            if dtp_rec:
                fields = getattr(dtp_rec, di_name).get_dfs()
                if fields:
                    for df in fields:
                        df.make_input()
        else:
            dtp_rec = di.get_dtp_record()
            if dtp_rec:
                fields = getattr(dtp_rec, di_name).get_dfs()
                if fields:
                    for df in fields:
                        df.make_output()
                    
    return di.get_value()

computeOutput.deps = ("DI060098", "DI060069", "DI062203","*")

def processInput(di, oldval):
    
    dependend_dfs = {"dlg_closebtn",
                     "dlg_movable"
                     }
    # if dlg_show_title is true, dlg_closebtn and dlg_movable will be available for the user
    # if dlg_show_title is false, they won't be available (output)

    for di_name in dependend_dfs:
        if di.get_value():
            for df in getattr(di.get_dtp_record(), di_name).get_dfs():
                    df.make_input()
        else:
            for df in getattr(di.get_dtp_record(), di_name).get_dfs():
                    df.make_output()
                    
    return di.get_value()

processInput.deps = ("DI060098","DI060069","DI062203")'''
BEFORE_HASHES = ('011b105c62b71a0950e0a0961e5a49da', '2fda14553d1265453c6c5c83b15875c7')
AFTER_HASH = '8bd5513f3f806d8313137d4860993145'

class ChangeDialogAvailability(ChangeDTPPacket, MigratePythonPacket):
    '''Change which fields are available, depending on DI 060098'''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9989

    def get_dependencies(self):
        return (MinVersionDependency(version='39.4'),
                MaxVersionDependency(version='39.4'))

    def fix(self, runner):
        fix_func = lambda _: CODE

        for before_hash in BEFORE_HASHES:
            if self.migrate_python(before_hash, AFTER_HASH,
                                       self.CU_TYPE_DATAITEM, '060098', fix_func):
                return self.success()

        return self.fail()
