package puzzle;

import lombok.val;
import org.junit.jupiter.api.Test;
import puzzle.Export.Signa;
import puzzle.Export.Puzzle;
import puzzle.Export.Placed;
import puzzle.Export.PuzzleResult;
import puzzle.Export.Rewards;
import puzzle.SwedishGenerator.Assign;
import puzzle.SwedishGenerator.FillResult;
import puzzle.SwedishGenerator.Lemma;
import puzzle.SwedishGenerator.Rng;
import puzzle.SwedishGenerator.Slotinfo;
import puzzle.dict950.DictData950;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static precomp.Const9x8.*;
import static precomp.Const9x8.Cell.*;
import static puzzle.GridBuilder.placeWord;
import static puzzle.LemmaData.TEST;
import static puzzle.Masker.C;
import static puzzle.Masker.R;
import static puzzle.Masker.STACK_SIZE;

public class MarkerTest {
   
   @Test
   void testValidRandomMask() {
      var rng    = new Rng(42);
      var masker = new Masker(rng, new int[STACK_SIZE], Clues.createEmpty());
      for (var i = 0; i < 200; i++) {
         for (var j = 19; j < 24; j++) {
            var clues = masker.randomMask(j);
            assertTrue(masker.isValid(clues), "Mask should be valid for length \n" + new Signa(clues).gridToString());
         }
      }
   }
   @Test
   void testValidMutate() {
      var rng      = new Rng(42);
      var cache    = Clues.createEmpty();
      var masker   = new Masker(rng, new int[STACK_SIZE], cache);
      var sim      = 0.0;
      var simCount = 0.0;
      for (var i = 0; i < 200; i++) {
         for (var j = 19; j < 24; j++) {
            var clues = masker.randomMask(j);
            val orig  = cache.from(clues);
            simCount++;
            masker.mutate(clues);
            sim += Masker.similarity(orig, clues);
            assertTrue(masker.isValid(clues), "Mask should be valid for length \n" + new Signa(clues).gridToString());
         }
      }
      System.out.println("Average similarity: " + sim / simCount);
   }
   @Test
   void testCross() {
      var rng      = new Rng(42);
      var cache    = Clues.createEmpty();
      var masker   = new Masker(rng, new int[STACK_SIZE], cache);
      var sim      = 0.0;
      var simCount = 0.0;
      for (var i = 0; i < 200; i++) {
         for (var j = 19; j < 24; j++) {
            var clues  = masker.randomMask(j);
            var clues2 = masker.randomMask(j);
            simCount++;
            var cross = masker.crossover(clues, clues2);
            sim += Math.max(Masker.similarity(cross, clues), Masker.similarity(cross, clues2));
            assertTrue(masker.isValid(cross), "Mask should be valid for length \n" + new Signa(cross).gridToString());
         }
      }
      System.out.println("Average similarity: " + sim / simCount);
   }
   @Test
   void testSimilarity() {
      var a = Signa.of(r0c0d1, r2c1d0).c();
      var b = Signa.of(r0c0d1, r2c1d0).c();
      
      // Identity
      assertEquals(1.0, Masker.similarity(a, b), 0.001);
      
      // Different direction
      var c = Signa.of(r0c0d0, r2c1d0);
      assertTrue(Masker.similarity(a, c.c()) < 1.0);
      
      // Completely different
      var d = Clues.createEmpty();
      // Matching empty cells count towards similarity.
      // a has 2 clues, d has 0. They match on 70 empty cells.
      assertEquals(70.0 / 72.0, Masker.similarity(a, d), 0.001);
   }
   
   @Test
   void testIsValid() {
      var masker = new Masker(new Rng(42), new int[STACK_SIZE], Clues.createEmpty());
      assertTrue(masker.isValid(Clues.createEmpty()));
      
      // Valid clue: Right from (0,0) in 9x8 grid. Length is 8.
      assertTrue(masker.isValid(Signa.of(r0c0d1).c()));
      
      // Invalid clue: Right from (0,7) in 9x8 grid. Length is 1 (too short if MIN_LEN >= 2).
      assertFalse(masker.isValid(Signa.of(r0c7d1).c()));
   }
   
   @Test
   void testHasRoomForClue() {
      var g = Clues.createEmpty();
      
      // Room for Right clue at (0,0) (length 8)
      assertTrue(Masker.hasRoomForClue(g,r0c0d1.slotKey));
      
      // No room for Right clue at (0,8) (length 0 < MIN_LEN)
      assertFalse(Masker.hasRoomForClue(g,r0c8d1.slotKey));
      
      // Blocked room
      // Let's place a clue that leaves only 1 cell for another clue.
      g.setClue(r0c2d1);
      // Now Right at (0,0) only has (0,1) available -> length 1 < MIN_LEN (which is 2)
      assertFalse(Masker.hasRoomForClue(g,r0c0d1.slotKey));
      
      // But enough room
      g.clearClueLo(0L);
      g.setClue(r0c3d1);
      // Now Right at (0,0) has (0,1), (0,2) -> length 2 == MIN_LEN
      assertTrue(Masker.hasRoomForClue(g,r0c0d1.slotKey));
   }
   
   @Test
   void testIntersectionConstraint() {
      var masker = new Masker(new Rng(42), new int[STACK_SIZE], Clues.createEmpty());
      // Clue 1: (0,0) Right. Slot cells: (0,1), (0,2), (0,3), (0,4), (0,5), (0,6), (0,7), (0,8)
      // Clue 2: (1,2) Up. Slot cells: (0,2)
      // Intersection is exactly 1 cell (0,2). Valid.
      assertTrue(masker.isValid(Signa.of(r0c0d1, r2c2d2).c()));
      
      // Clue 3: (1,1) Right. Slot cells: (1,2), (1,3), ...
      // No intersection with Clue 1 or 2. Valid.
      assertTrue(masker.isValid(Signa.of(r0c0d1, r2c2d2, r1c1d1).c()));
      
      // Now create a violation: two slots sharing 2 cells.
      // We can do this with Corner Down and another clue.
      // Clue A: (0,0) Corner Down. Starts at (0,1) goes down: (0,1), (1,1), (2,1), (3,1), ...
      // Clue B: (0,2) Corner Down Left. Starts at (0,1) goes down: (0,1), (1,1), (2,1), ...
      // They share MANY cells starting from (0,1).
      assertFalse(masker.isValid(Signa.of(r0c0d4, r0c2d5).c()));
   }
   
   @Test
   void testInvalidDirectionBits() {
      var masker = new Masker(new Rng(42), new int[STACK_SIZE], Clues.createEmpty());
      var g      = Clues.createEmpty();
      // Dir 6 (x=1, r=1, v=0) is invalid
      g.setClueLo(1L << 0, (byte) 6);
      assertFalse(masker.isValid(g));
      
      // Dir 7 (x=1, r=1, v=1) is invalid
      var g2 = Clues.createEmpty();
      g2.setClueLo(1L << 0, (byte) 7);
      assertFalse(masker.isValid(g2));
   }
   @Test
   void testConnectivityPenalty() {
      var rng    = new Rng(42);
      var masker = new Masker(rng, new int[STACK_SIZE], Clues.createEmpty());
      
      // 1. Maak een masker met één component van clues (bijv. 2 clues die elkaar kruisen)
      var singleComp = Clues.createEmpty().setClue(r0c0d1).setClue(r2c2d2);
      
      var fitnessSingle = masker.maskFitness(singleComp, 2);
      
      // 2. Maak een masker met twee eilandjes van clues
      var twoIslands = Clues.createEmpty().setClue(r0c0d1).setClue(r7c7d1);
      
      var fitnessIslands = masker.maskFitness(twoIslands, 2);
      
      System.out.println("[DEBUG_LOG] Fitness single component: " + fitnessSingle);
      System.out.println("[DEBUG_LOG] Fitness two islands: " + fitnessIslands);
      
      assertTrue(fitnessIslands > fitnessSingle + 10000, "Islands should have much higher penalty");
   }
   
   @Test
   void testPhysicalAdjacency() {
      var rng    = new Rng(42);
      var masker = new Masker(rng, new int[STACK_SIZE], Clues.createEmpty());
      
      // Twee clues naast elkaar, maar slots kruisen niet.
      // Clue 1: (1,1) Right. Slot (1,2), (1,3), (1,4)
      // Clue 2: (2,1) Right. Slot (2,2), (2,3), (2,4)
      var clues = Clues.createEmpty().setClue(r1c1d1).setClue(r2c1d1);
      
      var fitness = masker.maskFitness(clues, 2);
      // Als 8-naburigheid NIET MEER meetelt, moet de penalty hoog zijn.
      System.out.println("[DEBUG_LOG] Fitness physically adjacent: " + fitness);
      assertTrue(fitness > 20000, "Should have island penalty even if physically adjacent");
      
      // Twee clues naast elkaar, maar slots kruisen niet.
      var clues2   = Clues.createEmpty().setClue(r1c1d1).setClue(r3c1d1);
      var fitness2 = new Masker(rng, new int[STACK_SIZE], Clues.createEmpty()).maskFitness(clues2, 2);
      // Als 8-naburigheid NIET MEER meetelt, moet de penalty hoog zijn.
      System.out.println("[DEBUG_LOG] Fitness physically adjacent: " + fitness2);
      assertTrue(fitness2 > 20000, "Should have island penalty even if physically adjacent");
   }
   
   @Test
   void testCornerClueConnectivity() {
      var rng    = new Rng(42);
      var masker = new Masker(rng, new int[STACK_SIZE], Clues.createEmpty());
      
      // Clue A: (2,0) Right. Slot: (2,1), (2,2), (2,3), ...
      // Clue B: (1,2) Corner Down. Word starts at (1,3) en gaat omlaag: (1,3), (2,3), (3,3)...
      // Ze kruisen op (2,3).
      var clues = Clues.createEmpty().setClue(r2c0d1).setClue(r1c2d4);
      
      var fitness = masker.maskFitness(clues, 2);
      System.out.println("[DEBUG_LOG] Fitness corner clue connected: " + fitness);
      
      // Als ze verbonden zijn, is de penalty voor eilandjes 0.
      // We vergelijken met een island scenario (2 clues die elkaar NIET raken)
      var island = Clues.createEmpty().setClue(r2c0d1).setClue(r7c7d1);
      
      var fitnessIsland = masker.maskFitness(island, 2);
      System.out.println("[DEBUG_LOG] Fitness island: " + fitnessIsland);
      
      assertTrue(fitnessIsland > fitness + 20000, "Island should add significant penalty compared to connected corner clue");
   }
   @Test
   void testCornerDownSlot() {
      var clues = Signa.of(r0c0d4);
      // Clue op (0,0), type 4 (Corner Down)
      
      assertEquals(r0c0d4.d, clues.getDir(r0c0d4.index));
      
      // Controleer of forEachSlot het slot vindt
      final var found = new boolean[]{ false };
      Masker.forEachSlot(clues.c(), (key, lo, hi) -> {
         if (key == r0c0d4.slotKey) {
            found[0] = true;
            // Woord zou moeten starten op (0,1)
            assertTrue((lo & (1L << OFF_0_1)) != 0, "Slot should start at (0,1)");
            // En omlaag gaan
            assertTrue((lo & (1L << OFF_1_1)) != 0, "Slot should continue to (1,1)");
            
            // Lengte van het slot zou 8 moeten zijn (van rij 0 t/m 7 in kolom 1)
            assertEquals(8, Masker.Slot.length(lo, hi));
         }
      });
      assertTrue(found[0], "Corner Down slot should be found");
   }
   
   @Test
   void testCornerDownExtraction() {
      var slots = Masker.slots(Signa.of(r0c0d4).c(), DictData950.DICT950);
      assertEquals(1, slots.length);
      assertEquals(r0c0d4.d, Masker.Slot.dir(slots[0].key()));
   }
   
   @Test
   void testCornerDownLeftSlot() {
      var clues = Signa.of(r0c1d5);
      
      assertEquals(r0c1d5.d, clues.getDir(r0c1d5.index));
      
      // Controleer of forEachSlot het slot vindt
      final var found = new boolean[]{ false };
      Masker.forEachSlot(clues.c(), (key, lo, hi) -> {
         if (key == r0c1d5.slotKey) {
            found[0] = true;
            // Woord zou moeten starten op (0,0)
            assertTrue((lo & (1L << OFF_0_0)) != 0, "Slot should start at (0,0)");
            // En omlaag gaan
            assertTrue((lo & (1L << OFF_1_0)) != 0, "Slot should continue to (1,0)");
            
            // Lengte van het slot zou 8 moeten zijn (van rij 0 t/m 7 in kolom 0)
            assertEquals(8, Masker.Slot.length(lo, hi));
         }
      });
      assertTrue(found[0], "Corner Down Left slot should be found");
   }
   
   @Test
   void testCornerDownLeftExtraction() {
      var slots = Signa.of(r0c1d5).slots(DictData950.DICT950);
      
      assertEquals(1, slots.length);
      assertEquals(r0c1d5.d, Masker.Slot.dir(slots[0].key()));
   }
   @Test
   void testExportFormatFromFilled() {
      val clues = Signa.of(r0c0d1, r0c5d3);
      var grid  = new Puzzle(clues);
      
      // key = (cellIndex << 2) | (direction)
      var key = r0c0d1.slotKey;
      var lo  = (1L << OFF_0_1) | (1L << OFF_0_2) | (1L << OFF_0_3) | (1L << OFF_0_4);
      
      assertTrue(placeWord(grid.grid(), grid.grid().g, key, lo, 0L, TEST));
      
      var fillResult = new FillResult(true, 0, 0, 0, 0);
      var puzzleResult = new PuzzleResult(clues, grid, new Slotinfo[]{
              new Slotinfo(key, lo, 0L, 0, new Assign(TEST), null, 0)
      }, fillResult);
      
      var rewards  = new Rewards(10, 5, 1);
      var exported = puzzleResult.exportFormatFromFilled(rewards);
      
      assertNotNull(exported);
      assertEquals(709, exported.difficulty());
      assertEquals(rewards, exported.rewards());
      
      // Check words
      assertEquals(1, exported.words().length);
      var w = exported.words()[0];
      assertEquals("TEST", w.word());
      assertEquals(Placed.HORIZONTAL, w.direction());
      
      // The bounding box should include (0,0) for the arrow and (0,1)-(0,4) for the word.
      // minR=0, maxR=0, minC=0, maxC=4
      // startRow = 0 - minR = 0
      // startCol = 1 - minC = 1
      assertEquals(0, w.startRow());
      assertEquals(1, w.startCol());
      assertEquals(0, w.arrowRow());
      assertEquals(0, w.arrowCol());
      
      // Check gridv2
      // It should be 1 row, containing "2TEST" -> but letters are mapped, digits are not explicitly in letterAt.
      // Wait, look at exportFormatFromFilled logic:
      // row.append(letterAt.getOrDefault(pack(r, c), '#'));
      // letterAt only contains letters from placed words.
      // arrow cells are NOT in letterAt unless they are also part of a word (unlikely).
      // So (0,0) should be '#'
      assertEquals(1, exported.grid().length);
      assertEquals("#TEST", exported.grid()[0]);
   }
   
   @Test
   void testExportFormatEmpty() {
      var grid         = SwedishGeneratorTest.createEmpty9x8();
      val clues        = Clues.createEmpty();
      var fillResult   = new FillResult(true, 0, 0, 0, 0);
      var puzzleResult = new PuzzleResult(new Signa(clues), new Puzzle(grid, clues), new Slotinfo[0], fillResult);
      
      var exported = puzzleResult.exportFormatFromFilled(new Rewards(0, 0, 0));
      
      assertNotNull(exported);
      assertEquals(0, exported.words().length);
      // Should return full grid with '#'
      assertEquals(R, exported.grid().length);
      for (var row : exported.grid()) {
         assertEquals(C, row.length());
         assertTrue(row.matches("#+"));
      }
   }
   
   @Test
   void testShardToClue() {
      var bytes = Export.BYTES.get();
      for (var length = 2; length <= 8; length++) {
         val entry = DictData950.DICT950.index()[length];
         if (entry == null) continue;
         val words = entry.words();
         for (var i = 0; i < Math.min(words.length, 5); i++) {
            val wordVal = words[i];
            
            val rec = Meta.lookupSilent(wordVal);
            
            assertNotNull(rec);
            assertEquals(Lemma.asWord(wordVal, bytes), Lemma.asWord(rec.w(), bytes));
            assertTrue(rec.simpel() >= 0);
            assertTrue(rec.clues().length > 0);
         }
      }
   }
   
   @Test
   void testSpecificWords() {
      // These words are known to be in the CSV and likely in the dictionary
      var bytes     = Export.BYTES.get();
      var testWords = new String[]{ "EEN", "NAAR", "IEDEREEN" };
      for (var wStr : testWords) {
         var w = Lemma.from(wStr);
         
         val clueRec = Meta.lookupSilent(w);
         assertNotNull(clueRec);
         assertEquals(wStr, Lemma.asWord(clueRec.w(), bytes));
         // Check some expected complexity values (from CSV head output, column 3)
         if (wStr.equals("EEN")) {
            assertEquals(451, clueRec.simpel());
            assertEquals("een geheel vormend", clueRec.clues()[0]);
         }
         if (wStr.equals("NAAR")) {
            assertEquals(497, clueRec.simpel());
            assertEquals("onaangenaam, vervelend, rot, niet leuk", clueRec.clues()[0]);
         }
         if (wStr.equals("IEDEREEN")) {
            assertEquals(501, clueRec.simpel());
            assertEquals("elke persoon", clueRec.clues()[0]);
         }
         
         assertTrue(clueRec.clues().length > 0);
      }
   }
}
