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 bool selfDetonate;
    public float selfDetonateTime;
    private TimerHelper _selfDetonateTimer;

    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");
        _selfDetonateTimer = new TimerHelper(selfDetonateTime, false);
    }

    // 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 (selfDetonate)
            {
                if (_selfDetonateTimer.HasTicked(Time.deltaTime))
                {
                    MineColliderManager currentMineCM = gameObject.GetComponent<MineColliderManager>();
                    if (currentMineCM != null)
                    {
                        currentMineCM.HitMine();
                    }
                    _selfDetonateTimer.RestartTimer();
                }
                else
                {
                    mineModel.GetComponent<MeshRenderer>().materials[0].SetColor("_Color", Color.Lerp(_defaultMineColor, colorFlash, _selfDetonateTimer.Position));
                    mineModel.GetComponent<MeshRenderer>().materials[0].SetColor("_EmissionColor", Color.Lerp(_defaultMineEmissionColor, colorFlash, _selfDetonateTimer.Position));                    
                }
            }
        } 
    }     
}
