# -*- coding: utf-8 -*-
import re

from ppms.migration import *

BEFORE_HASHES = ('d97968e04621f00ab516b3891b676ec9', '871fb725667b319490fac1882ba5e865')
AFTER_HASHES = ('62e0dd0399e77e1128b64f787d4420d1', 'bb6e31c690de6e5747b42514d59e44d3')

def sub(code):
    code = re.sub(r'(?:"dict_key_value": "(728061)")', '"dict_key_value": "728059"', code, 1)
    code = code.replace('[get_df_value(rec,settings["dict_key_value"])]', '[get_df_value(rec,settings["dict_key_value"]) + da_id]')

    return code

class FixMMSChartCode(MigratePythonPacket):
    '''Fixes overlapping master milestones not showing up in their Chart

    If multiple master milestones had the same end date they would not show up
    in the legend of the MMS chart in the project dashboard.

    This packet may fail because the chart was individualized for a customer,
    in that case you need to apply the fix manually:

    Replace "dict_key_value: 728061" with "dict_key_value: 728059" in
    DA 050680 (the field number of the DF in question is 728914). After that
    replace [get_df_value(rec,settings["dict_key_value"])] with
    [get_df_value(rec,settings["dict_key_value"]) + da_id]
    '''
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 9420

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

    def fix(self, runner):
        for before_hash, after_hash in zip(BEFORE_HASHES, AFTER_HASHES):
            if self.migrate_python(before_hash, after_hash,
                                   self.CU_TYPE_IRONPYTHON, ['050680', 728914],
                                   sub):
                return self.success()

        return self.fail('See above')



