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

PACKET_PATH = 'migration.hotfix.S_39_5_4.wi11533_port_universal_interface_VoE.PortCustomizingUniversalInterface'

TEXT_CONSTANT = {'FR': ("%d envoyé - %d réçu - %d n'a pas fonctionné",
                        "%d / %s envoyé - %d réçu - %d n'a pas fonctionné"),
                 'EN': ("%d sent - %d received - %d failed",
                        "%d / %s sent - %d received - %d failed"),
                 'PT': ("%d enviado - %d recebido - %d falhado",
                        "%d / %s enviado - %d recebido - %d falhado")}


class UpdatePLANTALinkTextConstant(ChangeDTPPacket):
    """Update a text constant in non-german languages in the PLANTA Link"""
    version = 1
    run_on_startup = True
    done_after_success = True
    category = PacketCategory.CATEGORY_MANDATORY
    wi_number = 13334

    def get_dependencies(self):
        return (PacketDependency(packet_path=PACKET_PATH, expected_state=PacketState.INSTALLED),
                MaxVersionDependency(version='39.5.3', version_type=VersionType.CUSTOMIZING))

    def fix(self, runner):
        text_constant_record = self.get_record(425, ['001591'], ['title'])
        i_text_number = text_constant_record.title.get_tech_value()

        failures = 0
        for language, (old_text, new_text) in TEXT_CONSTANT.items():
            i_text_record = self.get_record(435, [i_text_number, language], ['i_text'])
            if not self.change_value(record=i_text_record, old_value=old_text, new_value=new_text, attribute='i_text'):
                failures += 1

        if failures:
            return self.fail()

        return self.success()