using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using SVGImporter;

[System.Serializable]
public class ImageSet {
    public SVGAsset normal;
    public SVGAsset flooded;
    public SVGAsset destroyed;
}

[SelectionBase]
public class Tile : MonoBehaviour, IPointerClickHandler {

	public enum TileState {
		NORMAL, FLOODED, DESTROYED
	}

    public List<ActionAsset> eligibleActions;
    public SVGRenderer svgRenderer;
    public SVGRenderer background;
    public ImageSet imageSet;
    public float chanceToStickWithDefaultSet = 0;

    public List<ImageSet> imageSets = new List<ImageSet>();

    public float waterLevel;
	public float waterAddition;
	public uint waterCapacity;
	public bool isWater;
	public bool isTree;
	public bool isBarrier;
	public bool canBeRepaired;
	public uint waterRetention;
	public uint waterAbsorption;

	public float waterDiff;
	public float waterCurrentX;
	public float waterCurrentY;

	public float newWaterCurrentX;
	public float newWaterCurrentY;

	public static Color brownColor = new Color(142 / 255.0f,10 / 255.0f,0);
	public static Color blueColor = new Color(0x5e / 255.0f, 0xa9 / 255.0f, 0xd3 / 255.0f);
	public static Color whiteColor = new Color(0xf1 / 255.0f, 0xff / 255.0f, 0xc5 / 255.0f);

	public uint tileX;
	public uint tileY;

	public TileState tileState = TileState.NORMAL;

	public float lastColorUpdate;
	private float lastColorLevel = -1000.0f;

    protected virtual void Awake () {

        if (Random.value >= chanceToStickWithDefaultSet)
        {
            if (imageSets.Count > 0)
                imageSet = imageSets [Random.Range(0, imageSets.Count)];
        }

        svgRenderer.vectorGraphics = imageSet.normal;
		lastColorUpdate = 10.0f;
        UpdateColor();
		if(isWater){
			background.color = blueColor;
		}
    }

    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button != PointerEventData.InputButton.Left)
            return;

        if (!UIController.Instance.actionsRadial.opened)
            UIController.Instance.actionsRadial.Open(eligibleActions, this);
        else
            UIController.Instance.actionsRadial.Close();

    }
    
	public bool isOverloaded{
		get{
			return waterLevel > waterCapacity;
		}
	}

	public bool isFull{
		get{
			return waterLevel >= waterCapacity;
		}
	}

	public bool isAbsorbing{
		get{
			return waterLevel < waterAbsorption;
		}
	}

	public bool isLikeWater {
		get {
			return isWater || waterLevel > 100f;
		}
	}

	public void UpdateColor()
    {
		Color src;
		if(isWater){
			return;
		}
		else {
			src = whiteColor;
		}

		float level = (waterLevel / 60.0f);
		bool doUpdate = (lastColorUpdate > 1.0f || Mathf.Abs(level - lastColorLevel) > 5.0f);
		if(GameController.Instance.floodState == GameController.FloodState.ENDED){
			doUpdate = doUpdate || level < lastColorLevel;
		}
		else {
			doUpdate = doUpdate || level < lastColorLevel;
		}

		if(isWater){
			level = level * 0.25f + 0.75f;
		}
		else {
			if(waterLevel > 70.0f && (tileState == TileState.FLOODED || canBeRepaired)){
                OnDestroyed();
				doUpdate = true;
			}
			else if(waterLevel > 40.0f && (tileState == TileState.NORMAL || canBeRepaired)){
                OnFlooded();
				doUpdate = true;
			}
			else if(canBeRepaired){
				OnNormal();
				doUpdate = true;
			}
		}

		if(doUpdate){
        	background.color = Color.Lerp(src, blueColor, level);
			lastColorUpdate = 0.0f;
			lastColorLevel = level;
		}
	}

    public virtual void OnFlooded()
    {
        svgRenderer.vectorGraphics = imageSet.flooded;
        tileState = TileState.FLOODED;
    }

    public virtual void OnDestroyed()
    {
        svgRenderer.vectorGraphics = imageSet.destroyed;
        tileState = TileState.DESTROYED;
    }

	public virtual void OnNormal()
	{
		svgRenderer.vectorGraphics = imageSet.normal;
		tileState = TileState.NORMAL;
	}

    public void UpdateSprite()
    {
        switch (tileState)
        {
            case TileState.NORMAL:
                svgRenderer.vectorGraphics = imageSet.normal;
                break;
            case TileState.FLOODED:
                svgRenderer.vectorGraphics = imageSet.flooded;
                break;
            case TileState.DESTROYED:
                svgRenderer.vectorGraphics = imageSet.destroyed;
                break;
        }
    }

	
}
