using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ComparePalace : MonoBehaviour {

    public int boardWidth = 6, boardHeight = 4, modelWidth = 3, modelHeight = 2;

    public Grid Grid;

    public GridToCopy GridToCopy;


       // Use this for initialization
    void Start () {
        /*Shapes[,] model = new Shapes[modelWidth, modelHeight];
        Shapes[,] board = new Shapes[boardWidth, boardHeight];

        model[0, 0] = Shapes.Rect;
        model[0, 1] = Shapes.Empty;
        model[1, 0] = Shapes.Rect;
        model[1, 1] = Shapes.Triangle;
        model[2, 0] = Shapes.RightTriangle;
        model[2, 1] = Shapes.Empty;

        board[0, 0] = Shapes.Rect;
        board[0, 1] = Shapes.Empty;
        board[1, 0] = Shapes.Rect;
        board[1, 1] = Shapes.Empty;
        board[2, 0] = Shapes.Rect;
        board[2, 1] = Shapes.Triangle;
        board[3, 0] = Shapes.RightTriangle;
        board[3, 1] = Shapes.Empty;

        Test(model, board, true);*/
    }

    public bool IsCorrect()
    {
        return CompareShapes(GridToCopy.GetCastle(), Grid.GetCastle());
    }

    public bool CompareShapes(ShapesEnum[,] model, ShapesEnum[,] board)
    {
        int xModelLeft = 0, xModelRight = modelWidth - 1, y = 0;

        // find left-most column with shapes on model;
        bool columnFound = false;
        while (xModelLeft < modelWidth && !columnFound)
        {
            for (y = 0; y < modelHeight; y++)
            {
                if (model[xModelLeft, y] != ShapesEnum.SHAPE_NONE)
                {
                    columnFound = true;
                    break;
                }
            }
            xModelLeft++;
        }
        if (!columnFound)
        {
            Debug.LogError("Sand Palace Model is Empty");
            return false;
        }
        xModelLeft--;

        // find right-most column
        columnFound = false;
        while (xModelRight >= xModelLeft && !columnFound)
        {
            for (y = 0; y < modelHeight; y++)
            {
                if (model[xModelRight, y] != ShapesEnum.SHAPE_NONE)
                {
                    columnFound = true;
                    break;
                }
            }
            xModelRight--;
        }
        xModelRight++;

        // scan the board, left to right. to find a match
        int properModelWidth = xModelRight - xModelLeft + 1;

        int xBoardOuter = 0, xInner = 0;
        bool mismatch = false;
        for (xBoardOuter = 0; xBoardOuter < boardWidth - properModelWidth + 1; xBoardOuter++)
        {
            //Debug.Log("Scan column:" + xBoardOuter);

            mismatch = false;
            // scan 1-1 match 
            for (xInner = 0; xInner < properModelWidth; xInner++)
            {
                // Assume modelHeight <= boardHeight
                for (y = 0; y < modelHeight; y++)
                {
                    if (board[xBoardOuter + xInner, y] != model[xModelLeft + xInner, y])
                    {
                        mismatch = true;
                        break;
                    }
                }
                if (mismatch)
                    break;
            } 
            if (!mismatch)
                return true;
        }
        return false;
    }

    public bool Test(ShapesEnum[,] model, ShapesEnum[,] board, bool result)
    {
        int x, y;
        if (CompareShapes(model, board) != result)
        {
            Debug.LogError("Failed Test");
            Debug.Log("Model: ");
            for (y = 0; y < modelHeight; y++)
            {
                string s = "";
                for (x = 0; x < modelWidth; x++)
                {
                    s = s + model[x, y] + ",";
                }
                Debug.Log(s);
            }
            Debug.Log("Board: ");
            for (y = 0; y < boardHeight; y++)
            {
                string s = "";
                for (x = 0; x < boardWidth; x++)
                {
                    s = s + board[x, y] + ",";
                }
                Debug.Log(s);
            }
            return false;
        }
        else
            Debug.Log("Test Passed");
        return true;
    }

}
