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

public class TailRootManeger : MonoBehaviour
{
    public enum TileType
    {
        Root,
        Plant
    }

    protected SpriteMask spriteMask;

    TileType tileType;

    protected SpriteRenderer spriteRenderer;
    protected Nodo node;
    CuadradoRaices cuadrado;

    public Sprite empty;

    public Sprite plantaRoot0;
    public Sprite plantaRoot01;
    public Sprite plantaRoot02;
    public Sprite plantaRoot03;
    public Sprite plantaRoot012;
    public Sprite plantaRoot013;
    public Sprite plantaRoot023;
    public Sprite plantaRoot024;

    public Sprite root0;
    public Sprite root1;
    public Sprite root2;
    public Sprite root3;
    public Sprite root01;
    public Sprite root03;
    public Sprite root12;
    public Sprite root23;
    public Sprite root02;
    public Sprite root13;
    public Sprite root013;
    public Sprite root213;
    public Sprite root102;
    public Sprite root302;
    public Sprite root402;


    void Start()
    {
        spriteRenderer = GetComponent<SpriteRenderer>();
        spriteMask = GetComponent<SpriteMask>();
        node = GetComponent<Nodo>();
        cuadrado = GetComponent<CuadradoRaices>();
    }

    protected virtual void Update()
    {
        spriteMask.sprite = spriteRenderer.sprite;
    }

    public virtual void SetTileSprite()
    {
        tileType = TileType.Root;
        if (cuadrado.objectsArround[0] != null) { if (cuadrado.objectsArround[0].tag == ("Plant")) { tileType = TileType.Plant; } }

        if (tileType == TileType.Plant)
        {
            if(CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = plantaRoot024;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = plantaRoot012;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = plantaRoot013;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = plantaRoot023;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]))
            { 
                spriteRenderer.sprite = plantaRoot01;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = plantaRoot02;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = plantaRoot03;
            }
            else if (CheckNotEmpty(node.entriesType[0]))
            {
                spriteRenderer.sprite = plantaRoot0;
            }
            else
            {
                spriteRenderer.sprite = empty;
            }
        }
        if (tileType == TileType.Root)
        {
            if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root402;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = root102;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root013;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root302;
            }
            else if (CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root213;
            }
            else if (CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root13;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = root02;
            }
            else if (CheckNotEmpty(node.entriesType[2]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root23;
            }
            else if (CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = root12;
            }
            else if (CheckNotEmpty(node.entriesType[0]) && CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root03;
            }
            else if (CheckNotEmpty(node.entriesType[1]) && CheckNotEmpty(node.entriesType[0]))
            {
                spriteRenderer.sprite = root01;
            }
            else if (CheckNotEmpty(node.entriesType[0]))
            {
                spriteRenderer.sprite = root0;
            }
            else if (CheckNotEmpty(node.entriesType[1]))
            {
                spriteRenderer.sprite = root1;
            }
            else if (CheckNotEmpty(node.entriesType[2]))
            {
                spriteRenderer.sprite = root2;
            }
            else if (CheckNotEmpty(node.entriesType[3]))
            {
                spriteRenderer.sprite = root3;
            }
            else
            {
                spriteRenderer.sprite = empty;
            }
        }
        if(node.m_PlantsController!=null) spriteRenderer.color = node.m_PlantsController.myColorHodeiEsteSi;
    }

    protected bool CheckNotEmpty(GridTypes type)
    {
        return type != GridTypes.Empty;
    }
}
