import datetime

from jinja2 import Template


def render_xml_od(offer_data_dict, agent_data, description, num_photos, option):
    get_actual_date = datetime.datetime.now().strftime("%Y-%m-%d")
    object_name = str(offer_data_dict.get("ObjectName", ""))

    is_flat = object_name == "0"
    is_house = object_name == "1"
    is_terrain = object_name == "2"
    is_commercial = object_name == "4"

    area_value = offer_data_dict.get("TerrainArea") if is_terrain else offer_data_dict.get("Area")

    contact_name = ""
    contact_phone = ""
    contact_email = ""
    agent_license = ""
    if agent_data:
        contact_name = f"{agent_data[4]} {agent_data[5]}".strip()
        contact_phone = str(agent_data[7]).replace(" ", "")
        contact_email = agent_data[6]
        agent_license = agent_data[8]

    xml_template_insert = """<?xml version="1.0" encoding="UTF-8"?>
<otoDom>
    <Agency>biuroinvestmentkk@gmail.com</Agency>
    <Date>{{ get_actual_date }}</Date>
    <ImportType>incremental</ImportType>
    <App>InvestmentManager</App>

    <Insertions>
        <Insertion>
            <ID>{{ offer_data.ID }}</ID>
            <Action>0</Action>
            <Country>1</Country>
            <Province>{{ offer_data.Province }}</Province>
            <District>{{ offer_data.District }}</District>
            <City>{{ offer_data.City }}</City>
            <Street>{{ offer_data.Street }}</Street>
            <Price>{{ offer_data.Price }}</Price>
            <PriceCurrency>1</PriceCurrency>
            <Area>{{ area_value }}</Area>
            <MarketType>{{ offer_data.MarketType }}</MarketType>
            <ObjectName>{{ offer_data.ObjectName }}</ObjectName>
            <OfferType>{{ offer_data.OfferType }}</OfferType>
            <Description>{{ description or offer_data.Description }}</Description>{% if has_agent %}
            <AgentLicense>{{ agent_license }}</AgentLicense>
            <ContactInfo>
                <Name>{{ contact_name }}</Name>
                <Phone>{{ contact_phone }}</Phone>
                <Email>{{ contact_email }}</Email>
            </ContactInfo>{% endif %}{% if is_flat %}
            <FlatDetails>
                <BuildingType>{{ offer_data.BuildingType_m }}</BuildingType>
                <BuildingFloorsNum>{{ offer_data.BuildingFloorsNum }}</BuildingFloorsNum>
                <FloorNo>{{ offer_data.FloorNo }}</FloorNo>
                <RoomsNum>{{ offer_data.RoomsNum }}</RoomsNum>
                <ConstructionStatus>{{ offer_data.ConstructionStatus_m }}</ConstructionStatus>
                <WindowsType>{{ offer_data.WindowsType_m }}</WindowsType>{% if offer_data.Heating %}
                <Heating>{{ offer_data.Heating }}</Heating>{% endif %}{% if offer_data.Furnished != '' %}
                <Furnished>{{ offer_data.Furnished }}</Furnished>{% endif %}{% if offer_data.Rent != '' %}
                <Rent>{{ offer_data.Rent }}</Rent>{% endif %}
            </FlatDetails>{% endif %}{% if is_house %}
            <HouseDetails>
                <BuildingType>{{ offer_data.BuildingType_d }}</BuildingType>
                <TerrainArea>{{ offer_data.TerrainArea }}</TerrainArea>
                <BuildYear>{{ offer_data.BuildYear }}</BuildYear>
                <RoomsNum>{{ offer_data.RoomsNum }}</RoomsNum>
                <FloorsNum>{{ offer_data.FloorsNum }}</FloorsNum>
                <ConstructionStatus>{{ offer_data.ConstructionStatus_d }}</ConstructionStatus>
                <WindowsType>{{ offer_data.WindowsType_d }}</WindowsType>{% if offer_data.MediaMask_d %}
                <MediaMask>{% for value in offer_data.MediaMask_d %}
                    <value>{{ value }}</value>{% endfor %}
                </MediaMask>{% endif %}{% if offer_data.HeatingMask %}
                <HeatingMask>{% for value in offer_data.HeatingMask %}
                    <value>{{ value }}</value>{% endfor %}
                </HeatingMask>{% endif %}{% if offer_data.Roofing %}
                <Roofing>{{ offer_data.Roofing }}</Roofing>{% endif %}
            </HouseDetails>{% endif %}{% if is_terrain %}
            <TerrainDetails>
                <Type>{{ offer_data.PlotType }}</Type>{% if offer_data.MediaMask_t %}
                <MediaMask>{% for value in offer_data.MediaMask_t %}
                    <value>{{ value }}</value>{% endfor %}
                </MediaMask>{% endif %}
            </TerrainDetails>{% endif %}{% if is_commercial %}
            <CommercialPropertyDetails>{% if offer_data.PropertyUseMask %}
                <PropertyUseMask>{% for value in offer_data.PropertyUseMask %}
                    <value>{{ value }}</value>{% endfor %}
                </PropertyUseMask>{% endif %}
                <ConstructionStatus>{{ offer_data.ConstructionStatus_l }}</ConstructionStatus>
            </CommercialPropertyDetails>{% endif %}
            <Photos>{% for i in range(num_photos) %}
                <Photo>
                    <File>image_{{ offer_data.ID }}_url_{{ i }}.jpg</File>
                </Photo>{% endfor %}
            </Photos>
        </Insertion>
    </Insertions>
</otoDom>"""

    xml_template_delete = """<?xml version="1.0" encoding="UTF-8"?>
<otoDom>
    <Agency>biuroinvestmentkk@gmail.com</Agency>
    <Date>{{ get_actual_date }}</Date>
    <ImportType>incremental</ImportType>
    <App>InvestmentManager</App>

    <Insertions>
        <Insertion>
            <ID>{{ offer_data.ID }}</ID>
            <Action>2</Action>
        </Insertion>
    </Insertions>
</otoDom>"""

    if option == "delete":
        template = Template(xml_template_delete)
        return template.render(offer_data=offer_data_dict, get_actual_date=get_actual_date)

    template = Template(xml_template_insert)
    return template.render(
        offer_data=offer_data_dict,
        num_photos=num_photos,
        description=description,
        get_actual_date=get_actual_date,
        has_agent=bool(agent_data),
        contact_name=contact_name,
        contact_phone=contact_phone,
        contact_email=contact_email,
        agent_license=agent_license,
        area_value=area_value,
        is_flat=is_flat,
        is_house=is_house,
        is_terrain=is_terrain,
        is_commercial=is_commercial,
    )
