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

public class MineObjectShared : ObjectShared
{
    public GameObject mineModel;
    public Color colorDefault;
    public Color colorFlash;
    private Color _defaultMineColor;
    private Color _defaultMineEmissionColor;

    public float flashLength;
    public float flashDistance;

    public float selfDetonateTime;
    private float _selfDetonateTick;

    private GameObject _player;

    private int _flashLevel;
    private float _flashTick;

    private void Start()
    {
        _defaultMineColor = mineModel.GetComponent<MeshRenderer>().materials[0].GetColor("_Color");
        _defaultMineEmissionColor = mineModel.GetComponent<MeshRenderer>().materials[0].GetColor("_EmissionColor");
        _flashLevel = 0;
        _player = GameObject.Find("Player");
    }

    // Helper class incase we need to track a variable over multiple scripts
    private void Update()
    {
        if (mineModel != null)
        {
            float playerDistance = (_player.transform.position - transform.position).magnitude;

            if (playerDistance <= flashDistance)
            {
                if (_flashTick >= flashLength)
                {
                    if (_flashLevel == 0)
                    {
                        mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_Color", colorFlash);
                        mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_EmissionColor", colorFlash);
                        _flashLevel = 1;
                    }
                    else
                    {
                        mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_Color", colorDefault);
                        mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_EmissionColor", colorDefault);
                        _flashLevel = 0;
                    }
                    _flashTick = 0.0f;
                }
                else
                {
                    _flashTick += Time.deltaTime;
                }
            }
            else
            {
                if (_flashLevel == 1)
                {
                    mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_Color", colorDefault);
                    mineModel.GetComponent<MeshRenderer>().materials[1].SetColor("_EmissionColor", colorDefault);
                    _flashLevel = 0;
                }
            }

            if (_selfDetonateTick >= selfDetonateTime)
            {
                MineColliderManager currentMineCM = gameObject.GetComponent<MineColliderManager>();
                if (currentMineCM != null)
                {
                    currentMineCM.HitMine();
                }                
                _selfDetonateTick = 0.0f;
            }
            else
            {
                mineModel.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", Color.Lerp(_defaultMineColor, colorFlash, (_selfDetonateTick / selfDetonateTime)));
                mineModel.GetComponent<MeshRenderer>().materials[0].SetColor("_EmissionColor", Color.Lerp(_defaultMineEmissionColor, colorFlash, (_selfDetonateTick / selfDetonateTime)));
                _selfDetonateTick += Time.deltaTime;
            }
        }
    }
}
