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


class AddNewDFBehaviours(ChangeDTPPacket):
    """Update the DF behaviour value range to allow the new "mi" and "mo" behaviours

    This packet does not port the entries for the listbox
    """
    done_after_success = True
    run_on_startup = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 18695

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

    def migrate_df_behaviour(self):
        di_record = self.get_record(dt_num=412, key_list=['000221'], di_list=['valuerange'])
        value_range = di_record.valuerange.get_value()

        if '"m1","m2","m3","m4","hy","py","h","ho","cc", "mi", "mo"]' in value_range:
            self.log('"mi" and "mo" are already allowed behaviours')
            return True
        else:
            new_value_range = value_range.replace('"m1","m2","m3","m4","hy","py","h","ho","cc"]',
                                                  '"m1","m2","m3","m4","hy","py","h","ho","cc", "mi", "mo"]')

            return self.change_value(record=di_record, attribute='valuerange',
                                     old_value=value_range, new_value=new_value_range)

    def fix(self, runner):
        packet_successful = self.migrate_df_behaviour()
        if not packet_successful:
            self.log('Valuerange of DI 000221 could not be updated.')
            self.log('Please add the behaviours "mi" and "mo" to the "allowed_behavior" list!')

            return self.fail('Please add the behaviours manually and set this packet to resolved')

        return self.success()
