from modelcommon.model import Store
from typing import Dict, Any
from pos_model import Order
from _NfceXmlPartBuilder import NfceXmlPartBuilder


class EmitBuilder(NfceXmlPartBuilder):
    def __init__(self, store_model):

        # type: (Store) -> None

        self.store_model = store_model

    def build_xml(self, order, context):
        # type: (Order, Dict[unicode, Any]) -> unicode

        # Dados Emitente
        # CRT: 1 = simples nacional, 2 = simples (excesso de sublimite de recieta), 3 = regime normal
        inner_xml = "<emit>" \
                    "<CNPJ>%s</CNPJ>" \
                    "<xNome>%s</xNome>" \
                    "<enderEmit>" \
                    "<xLgr>%s</xLgr>" \
                    "<nro>%s</nro>" % \
                    (
                        self.store_model.cnpj,
                        self.store_model.name,
                        self.store_model.address_street,
                        self.store_model.address_number
                    )

        if self.store_model.address_complement:
            inner_xml += "<xCpl>%s</xCpl>" % self.store_model.address_complement

        inner_xml += "<xBairro>%s</xBairro>" \
               "<cMun>%s</cMun>" \
               "<xMun>%s</xMun>" \
               "<UF>%s</UF>" \
               "<CEP>%s</CEP>" \
               "</enderEmit>" \
               "<IE>%s</IE>" \
               "<CRT>%d</CRT>" \
               "</emit>" % \
                     (
                         self.store_model.neighborhood,
                         self.store_model.city_code,
                         self.store_model.city,
                         self.store_model.state,
                         self.store_model.postal_code,
                         self.store_model.state_registration,
                         self.store_model.crt
                     )

        return inner_xml
