/*
 * RiskScape™ Copyright New Zealand Institute for Earth Science Limited
 * (Earth Sciences New Zealand) is distributed for research purposes only
 * under the terms of AGPLv3.
 *
 * RiskScape™ Copyright 2026 New Zealand Institute for Earth Science
 * Limited (Earth Sciences New Zealand). All rights reserved. Source code
 * available under the AGPLv3.
 * 
 * This program is free software: you can redistribute it and/or modify it under
 *  the terms of the GNU Affero General Public License as published by the Free
 *  Software Foundation, either version 3 of the License, or (at your option) any
 *  later version.
 * 
 * This program is distributed for RESEARCH PURPOSES ONLY, in the hope that it will
 * be useful for research and education initiatives.
 * 
 * If you are not a researcher, or you are a researcher who wishes to use this
 * program on terms other than AGPLv3 (including those who wish to restrict the
 * distribution of any source code created using this program), please contact:
 * https://riskscape.org.nz
 * 
 * This program is distributed WITHOUT ANY WARRANTY; without even the implied
 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Affero General Public License for more details.  You should have received a copy
 * of the GNU Affero General Public License along with this program.  If not, see
 * <http://www.gnu.org/licenses/>.
 * 
 * By way of summary only, under the AGPLv3:
 *     • Permissions of this strongest copyleft license are conditioned
 *       on making available complete source code of licensed works and
 *       modifications, which include larger works using a licensed work,
 *       under the same license.
 *     • Copyright and license notices must be preserved.
 *     • Contributors provide an express grant of patent rights.
 *     • When a modified version is used to provide a service over a
 *       network, the complete source code of the modified version must be made
 *       available.
 */
package nz.org.riskscape.engine.defaults.data;

import static org.hamcrest.Matchers.*;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

import org.geotools.api.referencing.crs.CoordinateReferenceSystem;
import org.junit.Before;
import org.junit.Test;
import org.locationtech.jts.geom.Coordinate;
import org.locationtech.jts.geom.GeometryFactory;
import org.locationtech.jts.geom.Point;

import nz.org.riskscape.engine.ProjectTest;
import nz.org.riskscape.engine.Tuple;
import nz.org.riskscape.engine.coverage.TypedCoverage;
import nz.org.riskscape.engine.geo.IntersectionIndex;
import nz.org.riskscape.engine.gt.GeometryHelper;
import nz.org.riskscape.engine.types.CoverageType;
import nz.org.riskscape.engine.types.Struct;
import nz.org.riskscape.engine.types.Struct.StructMember;
import nz.org.riskscape.engine.types.Types;

public class MosaicCoverageTest extends ProjectTest {

  GeometryHelper geomHelper = GeometryHelper.nzTransverseMercator(project.getSridSet());

  Struct indexStruct = Struct.of("geom", Types.GEOMETRY, "coverage", new CoverageType(Types.FLOATING));
  StructMember geomMember = indexStruct.getEntry("geom");
  StructMember coverageMember = indexStruct.getEntry("coverage");
  CoordinateReferenceSystem crs = geomHelper.nzTransverseMercator();
  GeometryFactory gf = project.getSridSet().getGeometryFactory(crs);

  IntersectionIndex index =
      new IntersectionIndex(geomMember, project.getSridSet(), IntersectionIndex.defaultOptions());

  TypedCoverage coverage1 = mock(TypedCoverage.class);
  TypedCoverage coverage2 = mock(TypedCoverage.class);

  MosaicCoverage subject;

  @Before
  public void setup() {
    index.insert(Tuple.ofValues(indexStruct, geomHelper.box(5, 5, 15, 15), coverage1));
    index.insert(Tuple.ofValues(indexStruct, geomHelper.box(10, 10, 20, 20), coverage2));
    index.build();

    subject = new MosaicCoverage(index, coverageMember, Types.FLOATING);
  }

  @Test
  public void sampleCentroidReturnsNullIfNoCoveragesIntersect() throws Exception {
    assertNull(subject.evaluate(gf.createPoint(new Coordinate(0, 0))));
  }

  @Test
  public void sampleCentroidSamplesAnIntersectingCoverage() {
    Point point = geomHelper.point(8, 8);
    when(coverage1.evaluate(point)).thenReturn(67D);
    assertThat(subject.evaluate(point), is(67D));

    // didn't intersect, didn't match
    verify(coverage2, never()).evaluate(any());
  }

  @Test
  public void sampleCentroidSamplesAllIntersectingCoveragesReturningANonNullValue() {
    // Point in overlapping region of both coverages
    Point point = geomHelper.point(12, 12);
    when(coverage1.evaluate(point)).thenReturn(null);
    when(coverage2.evaluate(point)).thenReturn(45D);

    assertThat(subject.evaluate(point), is(45D));

    // Both coverages should be sampled
    verify(coverage1).evaluate(point);
    verify(coverage2).evaluate(point);
  }

  @Test
  public void sampleCentroidSamplesAllIntersectingCoveragesReturningMaxValue() {
    // Point in overlapping region of both coverages
    Point point = geomHelper.point(12, 12);
    when(coverage1.evaluate(point)).thenReturn(30D);
    when(coverage2.evaluate(point)).thenReturn(45D);

    assertThat(subject.evaluate(point), is(45D));

    // Both coverages should be sampled
    verify(coverage1).evaluate(point);
    verify(coverage2).evaluate(point);
  }

  @Test
  public void sampleCentroidSamplesAllIntersectingCoveragesReturningNullIfAllCoveragesReturnNull() {
    // Point in overlapping region of both coverages
    Point point = geomHelper.point(12, 12);
    when(coverage1.evaluate(point)).thenReturn(null);
    when(coverage2.evaluate(point)).thenReturn(null);

    assertNull(subject.evaluate(point));

    // Both coverages should be sampled
    verify(coverage1).evaluate(point);
    verify(coverage2).evaluate(point);
  }
}
