from jinja2 import Template


def render_xml_dp(offer_data_dict, agent_data, description, num_photos, option):
    xml_template_insert = """<?xml version="1.0" encoding="UTF-8"?>
<Domiporta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Domiporta.xsd">
  <ExportSettings>
    <ExportType>Incremental</ExportType>
    <SchemaVersion>1.0.0</SchemaVersion>
  </ExportSettings>
  <Adverts>
    <Advert>
      <AdvertId>{{ offer_data.AdvertId }}</AdvertId>
      <TradeOperationType>{{ offer_data.TradeOperationType }}</TradeOperationType>
      <MarketType>{{ offer_data.MarketType }}</MarketType>
      <CategoryId>{{ offer_data.CategoryId }}</CategoryId>
      <Description><![CDATA[{{ description or offer_data.Description }}]]></Description>
      <Price>{{ offer_data.Price }}</Price>
      <PriceCurrency>{{ offer_data.PriceCurrency }}</PriceCurrency>
      <IsExclusiveOffer>{{ offer_data.IsExclusiveOffer }}</IsExclusiveOffer>
      <CountryName>{{ offer_data.CountryName }}</CountryName>
      <RegionName>{{ offer_data.RegionName }}</RegionName>{% if offer_data.CountyName %}
      <CountyName>{{ offer_data.CountyName }}</CountyName>{% endif %}{% if offer_data.CommunityName %}
      <CommunityName>{{ offer_data.CommunityName }}</CommunityName>{% endif %}{% if offer_data.CityName %}
      <CityName>{{ offer_data.CityName }}</CityName>{% endif %}{% if offer_data.StreetName %}
      <StreetName>{{ offer_data.StreetName }}</StreetName>{% endif %}{% if offer_data.GeoCodeLatitude %}
      <GeoCodeLatitude>{{ offer_data.GeoCodeLatitude }}</GeoCodeLatitude>{% endif %}{% if offer_data.GeoCodeLongitude %}
      <GeoCodeLongitude>{{ offer_data.GeoCodeLongitude }}</GeoCodeLongitude>{% endif %}
      <SubjectArea>{{ offer_data.SubjectArea }}</SubjectArea>{% if offer_data.PlotType %}
      <PlotType>{{ offer_data.PlotType }}</PlotType>{% endif %}{% if offer_data.RoomsCount and offer_data.CategoryId != '193' %}
      <RoomsCount>{{ offer_data.RoomsCount }}</RoomsCount>{% endif %}{% if offer_data.LivingArea %}
      <LivingArea>{{ offer_data.LivingArea }}</LivingArea>{% endif %}{% if offer_data.Floor %}
      <Floor>{{ offer_data.Floor }}</Floor>{% endif %}{% if offer_data.FloorsCount %}
      <FloorsCount>{{ offer_data.FloorsCount }}</FloorsCount>{% endif %}{% if offer_data.ConstructionYear %}
      <ConstructionYear>{{ offer_data.ConstructionYear }}</ConstructionYear>{% endif %}{% if offer_data.BuildingType %}
      <BuildingType>{{ offer_data.BuildingType }}</BuildingType>{% endif %}{% if offer_data.CommercialUsage %}
      <CommercialUsage>{% for usage_id in offer_data.CommercialUsage %}
        <UsageElement>{{ usage_id }}</UsageElement>{% endfor %}
      </CommercialUsage>{% endif %}{% if offer_data.PlotArea %}
      <PlotArea>{{ offer_data.PlotArea }}</PlotArea>{% endif %}{% if offer_data.Sewerage %}
      <Sewerage>{{ offer_data.Sewerage }}</Sewerage>{% endif %}{% if offer_data.Utilities %}
      <Utilities>{% for utility_id in offer_data.Utilities %}
        <UtilitiesElement>{{ utility_id }}</UtilitiesElement>{% endfor %}
      </Utilities>{% endif %}{% if offer_data.ParkingSpaceCount %}
      <ParkingSpaceCount>{{ offer_data.ParkingSpaceCount }}</ParkingSpaceCount>{% endif %}{% if offer_data.AdditionalInformation %}
      <AdditionalInformation>{% for info_id in offer_data.AdditionalInformation %}
        <AdditionalInformationElement>{{ info_id }}</AdditionalInformationElement>{% endfor %}
      </AdditionalInformation>{% endif %}{% if offer_data.Surroundings %}
      <Surroundings>{% for surroundings_id in offer_data.Surroundings %}
        <SurroundingsElement>{{ surroundings_id }}</SurroundingsElement>{% endfor %}
      </Surroundings>{% endif %}{% if offer_data.Amenity %}
      <Amenity>{% for amenity_id in offer_data.Amenity %}
        <AmenityElement>{{ amenity_id }}</AmenityElement>{% endfor %}
      </Amenity>{% endif %}{% if offer_data.AgentIdentifier %}
      <AgentIdentifier>{{ offer_data.AgentIdentifier }}</AgentIdentifier>{% endif %}{% if offer_data.AgentName %}
      <AgentName>{{ offer_data.AgentName }}</AgentName>{% endif %}{% if offer_data.AgentEmail %}
      <AgentEmail>{{ offer_data.AgentEmail }}</AgentEmail>{% endif %}{% if offer_data.AgentPhone %}
      <AgentPhone>{{ offer_data.AgentPhone }}</AgentPhone>{% endif %}{% if offer_data.BuildingState %}
      <BuildingState>{{ offer_data.BuildingState }}</BuildingState>{% endif %}{% if offer_data.Kitchen %}
      <Kitchen>{{ offer_data.Kitchen }}</Kitchen>{% endif %}{% if offer_data.Standard %}
      <Standard>{{ offer_data.Standard }}</Standard>{% endif %}
      <Pictures>
        <SourceType>{{ offer_data.PictureSourceType or "FileName" }}</SourceType>{% if offer_data.PicturePaths %}{% for picture_path in offer_data.PicturePaths %}
        <PicturePath>{{ picture_path|e }}</PicturePath>{% endfor %}{% else %}{% for i in range(num_photos) %}
        <PicturePath>image_{{ offer_data.AdvertId }}_url_{{ i }}.jpg</PicturePath>{% endfor %}{% endif %}
      </Pictures>
    </Advert>
  </Adverts>
</Domiporta>"""

    xml_template_delete = """<?xml version="1.0" encoding="UTF-8"?>
<Domiporta xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="Domiporta.xsd">
  <ExportSettings>
    <ExportType>Incremental</ExportType>
    <SchemaVersion>1.0.0</SchemaVersion>
  </ExportSettings>
  <AdvertManagment>
    <AdvertsToDelete>
      <AdvertId>{{ offer_data.AdvertId }}</AdvertId>
    </AdvertsToDelete>
  </AdvertManagment>
</Domiporta>"""

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

    template = Template(xml_template_insert)
    return template.render(
        offer_data=offer_data_dict,
        description=description,
        num_photos=num_photos,
    )
