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

public enum TeethStateType
{
    Good,
    Bad,
    Drop,
}

public enum TeethColorType
{
    White,
    Golden,
}

public class Teeth : MonoBehaviour
{
    private float DropSpeed = 8;
    private float DropEndpointY = -20;
    private GameObject good;
    private GameObject bad;
    private TeethStateType state;
    private TeethColorType color;
    private Vector3 originalPos;


    public TeethStateType TeethState { get { return state; } }
    public TeethColorType TeethColor { get { return color; } }

    // Start is called before the first frame update
    void Awake()
    {
        good = transform.Find("good").gameObject;
        bad = transform.Find("bad").gameObject;
        originalPos = transform.position;
    }

    private void Update()
    {
        if(state == TeethStateType.Drop)
        {
            if(Mathf.Abs(transform.localPosition.y - DropEndpointY) > 0.1f)
            {
                transform.position = new Vector3(transform.position.x, transform.position.y - 1 * DropSpeed * Time.deltaTime, transform.position.z);
            }
            else
            {
                state = TeethStateType.Bad;
                gameObject.SetActive(false);
            }
        }
    }

    public void ChangeState(TeethStateType state, bool isImmediate = false)
    {
        this.state = state;
        transform.position = originalPos;
        gameObject.SetActive(true);
        var render = bad.GetComponent<SpriteRenderer>();
        if (render != null) render.maskInteraction = SpriteMaskInteraction.VisibleInsideMask;

        if(state == TeethStateType.Good)
        {
            bad.gameObject.SetActive(false);
            good.gameObject.SetActive(true);
        }
        else
        {
            if (isImmediate)
            {
                gameObject.SetActive(false);
            }
            else
            {
                good.gameObject.SetActive(false);
                this.state = TeethStateType.Drop;
                bad.gameObject.SetActive(true);
                if (render != null)
                    render.maskInteraction = SpriteMaskInteraction.None;
            }
        }
    }

    public void SetColor(TeethColorType color)
    {
        this.color = color;
        var render = good.GetComponent<SpriteRenderer>();
        if(color == TeethColorType.Golden)
            render.color = Color.yellow;
        else
            render.color = Color.white;
    }
}
