<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <xsl:import href="MiniEditorCommon.xslt"/>
  <xsl:variable name="Method" select="//Form[@Code='MP']/Page[@Code='Common']/*/*[@Selected='true']/*[@Code='Method']"/>

  <xsl:template match="/MiniEditor/Forms">
    <Image xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      <xsl:variable name="OldNumber">
        <xsl:choose>
          <xsl:when test="$Method='1' or $Method='2' or $Method='3'">
            <xsl:for-each select="Form[@Code='OLZ' and @Selected='true']/Page[@Code='Parcels']/*[@Code='Parcel']/*">
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'CadastralNumber'"/>
              </xsl:call-template>
            </xsl:for-each>
          </xsl:when>
          <xsl:otherwise/>
        </xsl:choose>
      </xsl:variable>
      <xsl:variable name="PrefixNumber">
        <xsl:choose>
          <xsl:when test="$Method='1' or $Method='2' or $Method='3'">
            <xsl:value-of select="$OldNumber"/>
            <xsl:text>/</xsl:text>
          </xsl:when>
          <xsl:when test="$Method='4' or $Method='5' or $Method='6'">
            <xsl:text>:</xsl:text>
          </xsl:when>
          <xsl:otherwise>
            <xsl:text/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:variable>

      <xsl:for-each select="Form[@Code='NWZ' and @Selected='true']">
        <xsl:variable name="NWZID">
          <xsl:value-of select="@ID"/>
        </xsl:variable>
        <xsl:variable name="ParcelNumber">
          <xsl:value-of select="$PrefixNumber"/>
          <xsl:for-each select="Page[@Code='Parcels']/*[@Code='Parcel']/*">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'Designation'"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:variable>
        <Region>
          <xsl:attribute name="id">
            <xsl:value-of select="$ParcelNumber"/>
          </xsl:attribute>
          <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
            <xsl:for-each select="*">
              <xsl:variable name="Spat">
                <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
              </xsl:variable>
              <xsl:variable name="pos">
                <xsl:value-of select="position()"/>
              </xsl:variable>
              <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                <xsl:variable name="Spatial">
                  <xsl:choose>
                    <xsl:when test="$Spat=''">
                      <xsl:value-of select="'1'"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$Spat"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="Spatial_Draft">
                  <xsl:with-param name="Spatial" select="$Spatial"/>
                  <xsl:with-param name="pos" select="$pos"/>
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </xsl:for-each>
        </Region>

        <xsl:for-each select="../Form[@Code='NWZPart' and @Selected='true' and substring-after(@ParentID,'_')=$NWZID]/Page[@Code='SubParcels']/*[@Code='SubParcel']/*[@Selected='true']">
          <Region>
            <xsl:attribute name="id">
              <xsl:value-of select="$ParcelNumber"/>
              <xsl:text>/</xsl:text>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Designation'"/>
              </xsl:call-template>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Ord_Num'"/>
              </xsl:call-template>
            </xsl:attribute>
            <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']">
              <xsl:for-each select="*">
                <xsl:variable name="Spat">
                  <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
                </xsl:variable>
                <xsl:variable name="pos">
                  <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                  <xsl:variable name="Spatial">
                    <xsl:choose>
                      <xsl:when test="$Spat=''">
                        <xsl:value-of select="'1'"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$Spat"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <xsl:call-template name="Spatial_Draft">
                    <xsl:with-param name="Spatial" select="$Spatial"/>
                    <xsl:with-param name="pos" select="$pos"/>
                  </xsl:call-template>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </Region>
        </xsl:for-each>
      </xsl:for-each>

      <xsl:for-each select="Form[@Code='MDZ' and @Selected='true']">
        <xsl:variable name="MDZID">
          <xsl:value-of select="@ID"/>
        </xsl:variable>
        <xsl:for-each select="../Form[@Code='MDZPart' and @Selected='true' and substring-after(@ParentID,'_')=$MDZID]/Page[@Code='SubParcels']/*[@Code='SubParcel']/*[@Selected='true']">
          <Region>
            <xsl:attribute name="id">
              <xsl:value-of select="$OldNumber"/>
              <xsl:text>/</xsl:text>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Designation'"/>
              </xsl:call-template>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Ord_Num'"/>
              </xsl:call-template>
            </xsl:attribute>
            <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']">
              <xsl:for-each select="*">
                <xsl:variable name="Spat">
                  <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
                </xsl:variable>
                <xsl:variable name="pos">
                  <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                  <xsl:variable name="Spatial">
                    <xsl:choose>
                      <xsl:when test="$Spat=''">
                        <xsl:value-of select="'1'"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$Spat"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <xsl:call-template name="Spatial_Draft">
                    <xsl:with-param name="Spatial" select="$Spatial"/>
                    <xsl:with-param name="pos" select="$pos"/>
                  </xsl:call-template>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </Region>
        </xsl:for-each>
      </xsl:for-each>

      <xsl:for-each select="Form[@Code='CHZ' and @Selected='true']">
        <xsl:variable name="CHZID">
          <xsl:value-of select="@ID"/>
        </xsl:variable>
        <xsl:variable name="ParcelNumber">
          <xsl:for-each select="Page[@Code='Parcels']/*[@Code='Parcel']/*">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'CadastralNumber'"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:variable>
        <Region>
          <xsl:attribute name="id">
            <xsl:value-of select="$ParcelNumber"/>
            <xsl:text> (существующий)</xsl:text>
          </xsl:attribute>
          <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
            <xsl:for-each select="*">
              <xsl:variable name="Spat">
                <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
              </xsl:variable>
              <xsl:variable name="pos">
                <xsl:value-of select="position()"/>
              </xsl:variable>
              <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                <xsl:variable name="Spatial">
                  <xsl:choose>
                    <xsl:when test="$Spat=''">
                      <xsl:value-of select="'1'"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$Spat"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="Spatial_Draft">
                  <xsl:with-param name="Spatial" select="$Spatial"/>
                  <xsl:with-param name="pos" select="$pos"/>
                  <xsl:with-param name="code" select="0"/>
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </xsl:for-each>
        </Region>
        <Region>
          <xsl:attribute name="id">
            <xsl:value-of select="$ParcelNumber"/>
            <xsl:text> (уточненный)</xsl:text>
          </xsl:attribute>
          <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
            <xsl:for-each select="*">
              <xsl:variable name="Spat">
                <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
              </xsl:variable>
              <xsl:variable name="pos">
                <xsl:value-of select="position()"/>
              </xsl:variable>
              <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                <xsl:variable name="Spatial">
                  <xsl:choose>
                    <xsl:when test="$Spat=''">
                      <xsl:value-of select="'1'"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$Spat"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="Spatial_Draft">
                  <xsl:with-param name="Spatial" select="$Spatial"/>
                  <xsl:with-param name="pos" select="$pos"/>
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </xsl:for-each>
        </Region>
        <xsl:for-each select="../Form[@Code='CHZPart' and @Selected='true' and substring-after(@ParentID,'_')=$CHZID]/Page[@Code='SubParcels']/*[@Code='SubParcel']/*[@Selected='true']">
          <Region>
            <xsl:attribute name="id">
              <xsl:value-of select="$ParcelNumber"/>
              <xsl:text>/</xsl:text>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Designation'"/>
              </xsl:call-template>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Ord_Num'"/>
              </xsl:call-template>
            </xsl:attribute>
            <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']">
              <xsl:for-each select="*">
                <xsl:variable name="Spat">
                  <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
                </xsl:variable>
                <xsl:variable name="pos">
                  <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                  <xsl:variable name="Spatial">
                    <xsl:choose>
                      <xsl:when test="$Spat=''">
                        <xsl:value-of select="'1'"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$Spat"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <xsl:call-template name="Spatial_Draft">
                    <xsl:with-param name="Spatial" select="$Spatial"/>
                    <xsl:with-param name="pos" select="$pos"/>
                  </xsl:call-template>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </Region>
        </xsl:for-each>
      </xsl:for-each>

      <xsl:for-each select="Form[@Code='NBZ' and @Selected='true']">
        <xsl:variable name="NBZID">
          <xsl:value-of select="@ID"/>
        </xsl:variable>
        <xsl:variable name="ParcelNumber">
          <xsl:for-each select="Page[@Code='Parcels']/*[@Code='Parcel']/*">
            <xsl:call-template name="Value">
              <xsl:with-param name="x" select="'CadastralNumber'"/>
            </xsl:call-template>
          </xsl:for-each>
        </xsl:variable>
        <Pline>
          <xsl:attribute name="id">
            <xsl:value-of select="$ParcelNumber"/>
            <xsl:text> (существующий)</xsl:text>
          </xsl:attribute>
          <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
            <xsl:for-each select="*">
              <xsl:variable name="Spat">
                <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
              </xsl:variable>
              <xsl:variable name="pos">
                <xsl:value-of select="position()"/>
              </xsl:variable>
              <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                <xsl:variable name="Spatial">
                  <xsl:choose>
                    <xsl:when test="$Spat=''">
                      <xsl:value-of select="'1'"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$Spat"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="Spatial_Draft">
                  <xsl:with-param name="Spatial" select="$Spatial"/>
                  <xsl:with-param name="pos" select="$pos"/>
                  <xsl:with-param name="code" select="0"/>
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </xsl:for-each>
        </Pline>
        <Pline>
          <xsl:attribute name="id">
            <xsl:value-of select="$ParcelNumber"/>
            <xsl:text> (уточненный)</xsl:text>
          </xsl:attribute>
          <xsl:for-each select="Page[@Code='Entity_Spatial']/*[@Code='Point']">
            <xsl:for-each select="*">
              <xsl:variable name="Spat">
                <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
              </xsl:variable>
              <xsl:variable name="pos">
                <xsl:value-of select="position()"/>
              </xsl:variable>
              <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                <xsl:variable name="Spatial">
                  <xsl:choose>
                    <xsl:when test="$Spat=''">
                      <xsl:value-of select="'1'"/>
                    </xsl:when>
                    <xsl:otherwise>
                      <xsl:value-of select="$Spat"/>
                    </xsl:otherwise>
                  </xsl:choose>
                </xsl:variable>
                <xsl:call-template name="Spatial_Draft">
                  <xsl:with-param name="Spatial" select="$Spatial"/>
                  <xsl:with-param name="pos" select="$pos"/>
                </xsl:call-template>
              </xsl:if>
            </xsl:for-each>
          </xsl:for-each>
        </Pline>
        <xsl:for-each select="../Form[@Code='NBZPart' and @Selected='true' and substring-after(@ParentID,'_')=$NBZID]/Page[@Code='SubParcels']/*[@Code='SubParcel']/*[@Selected='true']">
          <Region>
            <xsl:attribute name="id">
              <xsl:value-of select="$ParcelNumber"/>
              <xsl:text>/</xsl:text>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Designation'"/>
              </xsl:call-template>
              <xsl:call-template name="Value">
                <xsl:with-param name="x" select="'Ord_Num'"/>
              </xsl:call-template>
            </xsl:attribute>
            <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']">
              <xsl:for-each select="*">
                <xsl:variable name="Spat">
                  <xsl:value-of select="substring-before(substring-after(Param[@Code='Num_Geopoint'],'('),')')"/>
                </xsl:variable>
                <xsl:variable name="pos">
                  <xsl:value-of select="position()"/>
                </xsl:variable>
                <xsl:if test="not(../*[position()=$pos]/preceding-sibling::*/Param[@Code='Num_Geopoint' and substring-before(substring-after(.,'('),')') = $Spat])">
                  <xsl:variable name="Spatial">
                    <xsl:choose>
                      <xsl:when test="$Spat=''">
                        <xsl:value-of select="'1'"/>
                      </xsl:when>
                      <xsl:otherwise>
                        <xsl:value-of select="$Spat"/>
                      </xsl:otherwise>
                    </xsl:choose>
                  </xsl:variable>
                  <xsl:call-template name="Spatial_Draft">
                    <xsl:with-param name="Spatial" select="$Spatial"/>
                    <xsl:with-param name="pos" select="$pos"/>
                  </xsl:call-template>
                </xsl:if>
              </xsl:for-each>
            </xsl:for-each>
          </Region>
        </xsl:for-each>
      </xsl:for-each>
    </Image>
  </xsl:template>

  <xsl:template name="Spatial_Draft">
    <xsl:param name="Spatial"/>
    <xsl:param name="pos"/>
    <xsl:param name="code"/>
    <Points>
      <xsl:for-each select="../../../Page[@Code='Entity_Spatial']/*[@Code='Point']/*[position()>=$pos]">
        <xsl:variable name="TestSUO00">
          <xsl:call-template name="Value">
            <xsl:with-param name="x" select="'Num_Geopoint'"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="TestSUO0" select="translate($TestSUO00,' ','')"/>
        <xsl:variable name="SpatialR">
          <xsl:choose>
            <xsl:when test="contains($TestSUO0,'(')">
              <xsl:value-of select="substring-before(substring-after($TestSUO0,'('),')')"/>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="1"/>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:variable>
        <xsl:if test="$Spatial=$SpatialR">
          <xsl:call-template name="Spatial_Draft_Line">
            <xsl:with-param name="code" select="$code"/>
          </xsl:call-template>
        </xsl:if>
      </xsl:for-each>
    </Points>
  </xsl:template>

  <xsl:template name="Spatial_Draft_Line">
    <xsl:param name="part"/>
    <xsl:param name="code"/>
    <xsl:variable name="X">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="concat('X',$code)"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="Y">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="concat('Y',$code)"/>
      </xsl:call-template>
    </xsl:variable>
    <Point>
      <xsl:attribute name="id">
        <xsl:call-template name="Value">
          <xsl:with-param name="x" select="'Point_Pref'"/>
        </xsl:call-template>
        <xsl:call-template name="Num_Geopoint"/>
      </xsl:attribute>
      <xsl:attribute name="x">
        <xsl:value-of select="$Y"/>
      </xsl:attribute>
      <xsl:attribute name="y">
        <xsl:value-of select="$X"/>
      </xsl:attribute>
    </Point>
  </xsl:template>

  <xsl:template name="Num_Geopoint">
    <xsl:variable name="TestSUO00">
      <xsl:call-template name="Value">
        <xsl:with-param name="x" select="'Num_Geopoint'"/>
      </xsl:call-template>
    </xsl:variable>
    <xsl:variable name="TestSUO0" select="translate($TestSUO00,' ','')"/>
    <xsl:choose>
      <xsl:when test="contains($TestSUO0,'(')">
        <xsl:value-of select="substring-before($TestSUO0,'(')"/>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$TestSUO0"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

</xsl:stylesheet>
