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

public class GridToCopy : MonoBehaviour {

    public class Pair
    {
        public int x;
        public int y;
        public float rand;

        public Pair(int x, int y, float rand)
        {
            this.x = x;
            this.y = y;
            this.rand = rand;
        }
    }

	public int Width;
	public int Height;

	public Sprite[] ShapesSprites;

	public Sprite NoShapeSprite;

	public Sprite QuestionSprite;

	public SpriteRenderer CubeToCopyObject;

	private SpriteRenderer[,] dropCubes;

	public string[] PredefinedCastles;

	private ShapesEnum[,] currentCastle;

	// Use this for initialization
	void Start () {
		dropCubes = new SpriteRenderer[Width, Height];

		for(int x = 0; x < Width; x++)
		{
			for(int y = 0; y < Height; y++)
			{
				dropCubes[x,y] = Instantiate(CubeToCopyObject);
				dropCubes[x,y].transform.SetParent(transform, false);
				dropCubes[x, y].transform.localPosition = new Vector3(x /** dropCubes[x, y].transform.localScale.x*/, y/** dropCubes[x, y].transform.localScale.y*/);
			}
		}

		RandomizeCastle();
	}

	public void RandomizeCastle()
	{
		currentCastle = ParseCastle(PredefinedCastles[Random.Range(0,PredefinedCastles.Length)]);
		for(int x = 0; x < Width; x++)
			for(int y = 0; y < Height; y++)
				dropCubes[x, y].sprite = ShapesSprites[(int)currentCastle[x, y]];
	}

	private ShapesEnum[,] ParseCastle(string castle)
	{
		string[] rows = castle.Split(new char[]{'-'}, System.StringSplitOptions.RemoveEmptyEntries);
		ShapesEnum[,] parsedCastle = new ShapesEnum[Width,Height];
		for(int row = 0; row < rows.Length; row++)
		{
			string[] cols = rows[row].Split(new char[]{','}, System.StringSplitOptions.RemoveEmptyEntries);
			for(int col = 0; col < cols.Length; col++)
			{
				parsedCastle[row, col] = (ShapesEnum) int.Parse(cols[col]);
			}
		}
		return parsedCastle;
	}

    public List<Pair> GetAllFilledPairs(){
        System.Random r = new System.Random();
        var result = new List<Pair>();
        for (int x = 0; x < Width; x++)
            for (int y = 0; y < Height; y++)
            {
                if (currentCastle[x,y]!= ShapesEnum.SHAPE_NONE)
                    result.Add(new Pair(x, y, r.Next()));
            }
        return result;
    }

    public void HideAll()
    {
        var pairs = GetAllFilledPairs();
        pairs.ForEach(pair => dropCubes[pair.x, pair.y].sprite = QuestionSprite);

        /*
        for (int x = 0; x < Width; x++)
			for(int y = 0; y < Height; y++)
				dropCubes[x, y].sprite = QuestionSprite;*/
    }

    public void HideSome()
    {
        var pairs = GetAllFilledPairs();
        pairs.OrderBy(pair => pair.rand).Take(pairs.Count / 2).ToList().ForEach(pair => dropCubes[pair.x, pair.y].sprite = QuestionSprite);

        /*
        for (int x = 0; x < Width; x++)
			for(int y = 0; y < Height; y++)
				if(Random.value <= 0.5)
					dropCubes[x, y].sprite = QuestionSprite;*/
    }

    // Update is called once per frame
    void Update () {
		if(Input.GetKeyDown(KeyCode.R))
			RandomizeCastle();
	}

	public ShapesEnum[,] GetCastle()
	{
		return currentCastle;
	}

}

