package fi.nls.oskari.wmts.domain;

import fi.nls.oskari.log.LogFactory;
import fi.nls.oskari.log.Logger;

/**
 * @see http://schemas.opengis.net/wmts/1.0/wmtsGetCapabilities_response.xsd
 */
public class TileMatrixLimits {

    private static Logger LOG = LogFactory.getLogger(TileMatrixLimits.class);

    private final TileMatrix tm;
    private final int minTileRow;
    private final int maxTileRow;
    private final int minTileCol;
    private final int maxTileCol;

    public TileMatrixLimits(TileMatrix tm, int minTileRow, int maxTileRow,
            int minTileCol, int maxTileCol) {
        this.tm = tm;
        this.minTileRow = minTileRow;
        this.maxTileRow = maxTileRow;
        this.minTileCol = minTileCol;
        this.maxTileCol = maxTileCol;
        validate();
    }

    /**
     * Just log as warnings instead of throwing exceptions for now
     * TODO: Don't accept invalid values
     */
    private void validate() {
        if (minTileRow < 0) {
            LOG.warn(tm.getId(), "Negative MinTileRow");
        }
        if (maxTileRow >= tm.getMatrixWidth()) {
            LOG.warn(tm.getId(), "MaxTileRow must be < MatrixWidth");
        }
        if (minTileRow > maxTileRow) {
            LOG.warn(tm.getId(), "MinTileRow must be < MaxTileRow");
        }
        if (minTileCol < 0) {
            LOG.warn(tm.getId(), "Negative MinTileCol");
        }
        if (maxTileCol >= tm.getMatrixHeight()) {
            LOG.warn(tm.getId(), "MaxTileCol must be < MatrixHeight");
        }
        if (minTileCol > maxTileCol) {
            LOG.warn(tm.getId(), "MinTileCol must be < MaxTileCol");
        }
    }

    public TileMatrix getTileMatrix() {
        return tm;
    }

    public int getMinTileRow() {
        return minTileRow;
    }

    public int getMaxTileRow() {
        return maxTileRow;
    }

    public int getMinTileCol() {
        return minTileCol;
    }

    public int getMaxTileCol() {
        return maxTileCol;
    }

}

@@ -104,16 +104,176 @@
 r;%0A%0A/**%0A
+ * Metadata describing the limits of a TileMatrix for a layer%0A *%0A * The schema and the specification are obviously erroneous - use common sense when validating%0A
  * @see 
@@ -1041,20 +1041,45 @@
 DO: 
-Don't
+Throw exceptions instead of
  accept
+ing
  inv
@@ -1188,33 +1188,24 @@
 m.getId(), %22
-Negative 
 MinTileRow%22)
@@ -1198,24 +1198,44 @@
  %22MinTileRow
+ must be nonNegative
 %22);%0A        
@@ -1248,45 +1248,114 @@
     
-if (maxTileRow %3E= tm.getMatrixWidth()
+// schema: positiveInteger%0A        // But probably should be nonNegativeInteger%0A        if (maxTileRow %3C 0
 ) %7B%0A
@@ -1411,21 +1411,19 @@
  be 
-%3C MatrixWidth
+nonNegative
 %22);%0A
@@ -1517,32 +1517,33 @@
 ileRow must be %3C
+=
  MaxTileRow%22);%0A 
@@ -1563,26 +1563,204 @@
     
-if (minTileCol %3C 0
+// schema: 'Maximim tile row index valid for this layer. From minTileRow to matrixWidth-1 ...'%0A        // Should be from minTileRow to matrixHeight-1%0A        if (maxTileRow %3E= tm.getMatrixHeight()
 ) %7B%0A
@@ -1797,27 +1797,41 @@
 ), %22
-Negative MinTileCol
+MaxTileRow must be %3C MatrixHeight
 %22);%0A
@@ -1832,32 +1832,33 @@
 ht%22);%0A        %7D%0A
+%0A
         if (maxT
@@ -1854,26 +1854,26 @@
    if (m
-ax
+in
 TileCol 
 %3E= tm.ge
@@ -1864,39 +1864,215 @@
 TileCol 
-%3E= tm.getMatrixHeight()
+%3C 0) %7B%0A            LOG.warn(tm.getId(), %22MinTileCol must be nonNegative%22);%0A        %7D%0A        // schema: positiveInteger%0A        // But probably should be nonNegativeInteger%0A        if (maxTileCol %3C 0
 ) %7B%0A    
@@ -2116,38 +2116,35 @@
 Col must be 
-%3C MatrixHeight
+nonNegative
 %22);%0A        
@@ -2238,16 +2238,17 @@
 ust be %3C
+=
  MaxTile
@@ -2260,24 +2260,312 @@
 ;%0A        %7D%0A
+        // schema: 'Maximim tile column index valid for this layer. From minTileCol to tileHeight-1 ...'%0A        // Should be from minTileCol to matrixWidth-1%0A        if (maxTileCol %3E= tm.getMatrixWidth()) %7B%0A            LOG.warn(tm.getId(), %22MaxTileCol must be %3C MatrixWidth%22);%0A        %7D%0A
     %7D%0A%0A    p

Fix TileMatrixLimits