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

public class Minimap : MonoBehaviour {

    public Image imageMinimap = null;
    public GameObject gameObjectMinimapViewport = null;

    public void Reset()
    {
        Color[] pixels = imageMinimap.sprite.texture.GetPixels();
        for (int i = 0; i < pixels.Length; i++)
            pixels[i] = Color.black;

        imageMinimap.sprite.texture.SetPixels(pixels);
    }

    private Vector2 TextureCoordsFromWorldPosition(Vector3 pos)
    {
        Vector2 result;
        TreeFieldPopulator tfp = GetComponent<TreeFieldPopulator>();
        result.x = 4 + (pos.x + tfp.XVariance) * (imageMinimap.sprite.texture.width - 8) / (tfp.XVariance * 2.0f);
        result.y = 4 + (pos.z + tfp.ZVariance) * (imageMinimap.sprite.texture.height - 8) / (tfp.ZVariance * 2.0f);

        return result;

    }

    private Vector2 MinimapImageCoordsFromTexturePosition(Vector2 pos)
    {
        Vector2 result;
        result.x = pos.x * imageMinimap.GetComponent<RectTransform>().rect.width / imageMinimap.sprite.texture.width - (imageMinimap.GetComponent<RectTransform>().rect.width / 2);
        result.y = pos.y * imageMinimap.GetComponent<RectTransform>().rect.height / imageMinimap.sprite.texture.height - (imageMinimap.GetComponent<RectTransform>().rect.height / 2); 

        return result;
    }

    private Color[] WaitingColors =
    {
        new Color(0.0f, 0.6f, 0.0f),
        new Color(0.0f, 0.7f, 0.0f),
        new Color(0.0f, 0.6f, 0.0f),

        new Color(0.0f, 0.7f, 0.0f),
        new Color(0.0f, 0.8f, 0.0f),
        new Color(0.0f, 0.7f, 0.0f),

        new Color(0.0f, 0.6f, 0.0f),
        new Color(0.0f, 0.7f, 0.0f),
        new Color(0.0f, 0.6f, 0.0f)
    };

    private Color[] BurningColors =
    {
        new Color(0.5f, 0.25f, 0.1f),
        new Color(0.6f, 0.3f, 0.1f),
        new Color(0.5f, 0.25f, 0.1f),

        new Color(0.6f, 0.3f, 0.1f),
        new Color(0.7f, 0.35f, 0.1f),
        new Color(0.6f, 0.3f, 0.1f),

        new Color(0.5f, 0.25f, 0.1f),
        new Color(0.6f, 0.3f, 0.1f),
        new Color(0.5f, 0.25f, 0.1f)
    };

    private Color[] BurntColors =
    {
        new Color(0.1f, 0.1f, 0.1f),
        new Color(0.15f, 0.15f, 0.15f),
        new Color(0.1f, 0.1f, 0.1f),

        new Color(0.15f, 0.15f, 0.15f),
        new Color(0.20f, 0.20f, 0.20f),
        new Color(0.15f, 0.15f, 0.15f),

        new Color(0.1f, 0.1f, 0.1f),
        new Color(0.15f, 0.15f, 0.15f),
        new Color(0.1f, 0.1f, 0.1f),
    };

    private Color[] StumpColors =
    {
        new Color(0.1f, 0.2f, 0.1f),
        new Color(0.15f, 0.25f, 0.15f),
        new Color(0.1f, 0.2f, 0.1f),

        new Color(0.15f, 0.25f, 0.15f),
        new Color(0.20f, 0.30f, 0.20f),
        new Color(0.15f, 0.25f, 0.15f),

        new Color(0.1f, 0.2f, 0.1f),
        new Color(0.15f, 0.25f, 0.15f),
        new Color(0.1f, 0.2f, 0.1f),
    };

    private Color[] RegularHouseColors =
    {
        new Color(0.8f, 0.8f, 0.8f),
        new Color(0.9f, 0.9f, 0.9f),
        new Color(0.8f, 0.8f, 0.8f),

        new Color(0.9f, 0.9f, 0.9f),
        new Color(0.95f, 0.95f, 0.95f),
        new Color(0.9f, 0.9f, 0.9f),

        new Color(0.8f, 0.8f, 0.8f),
        new Color(0.9f, 0.9f, 0.9f),
        new Color(0.8f, 0.8f, 0.8f),
    }; 

    private void ColorPixelsIntoMinimapTexture(Vector2 loc, Color[] colors)
    {
        if (loc.x > 1 && loc.x < imageMinimap.sprite.texture.width - 1 && loc.y > 1 && loc.y < imageMinimap.sprite.texture.height - 1)
        {
            imageMinimap.sprite.texture.SetPixels((int)loc.x - 1, (int)loc.y - 1, 3, 3, colors);
            imageMinimap.sprite.texture.Apply();
        }
    }

    public void DrawHouse(House house)
    {
        Vector2 texCoord = TextureCoordsFromWorldPosition(house.transform.position);
        switch (house.burningState)
        {
            case BurnState.Waiting:
                ColorPixelsIntoMinimapTexture(texCoord, RegularHouseColors);
                break;
            case BurnState.Smoking:
                break;
            case BurnState.Burning:
                ColorPixelsIntoMinimapTexture(texCoord, BurningColors);
                break;
            case BurnState.Smothered:
                ColorPixelsIntoMinimapTexture(texCoord, BurntColors);
                break;
        }

    }

    public void DrawTree(Tree tree)
    {
        Vector2 texCoord = TextureCoordsFromWorldPosition(tree.transform.position);

        switch (tree.burningState)
        {
            case BurnState.Waiting:
                ColorPixelsIntoMinimapTexture(texCoord, WaitingColors);
                break;
            case BurnState.Smoking:
                break;
            case BurnState.Burning:
                ColorPixelsIntoMinimapTexture(texCoord, BurningColors);
                break;
            case BurnState.Smothered:
                ColorPixelsIntoMinimapTexture(texCoord, BurntColors);
                break;
            case BurnState.Disabled:
                ColorPixelsIntoMinimapTexture(texCoord, StumpColors);
                break;
        }

    }

    public void ViewportMarkerLookAt(Vector3 worldPos)
    {
        Vector2 texturePos = TextureCoordsFromWorldPosition(worldPos);
        Vector2 imagePos = MinimapImageCoordsFromTexturePosition(texturePos);
        gameObjectMinimapViewport.transform.localPosition = imagePos;
    }

}
