using System.Collections;
using System.Collections.Generic;
using UnityEditor.VersionControl;
using UnityEngine;

public class PlayerController : MonoBehaviour
{
    public static PlayerController instance{get; private set;}
    public Sprite upwardSprite;
    public Sprite downwardSprite;
    public Sprite normalSprite;
    public ParticleSystem UpMoneyEffect;
    public Vector3 effectOffset;
    float defeaultEmitRate;
    public static Transform t {get {return instance.transform; }}
    void Awake(){
        defeaultEmitRate = UpMoneyEffect.emissionRate;
        UpMoneyEffect.emissionRate = 0;
        instance= this;
        spriteRenderer = GetComponent<SpriteRenderer>();
    }
    public Vector3 eulerAngles;
    public float movingSpeed = 1;
    public float boostSpeed= 2;
    public float turnAngle = 15;
    public GameObject trailPrefab;
    public Transform currentTrail;

    public float senseRadius = 5;

    public bool isUp { get { return transform.eulerAngles.z != 360-turnAngle;}}
    SpriteRenderer spriteRenderer;
    void Start()
    {
        CameraFollower.Target = transform;
    }

    bool gameStarted =false;

    public void GameOver(){
        Started=false;
        spriteRenderer.sprite = downwardSprite;
    }
    public static bool Started;
    // Update is called once per frame
    void Update(){
        UpMoneyEffect.transform.position = new Vector3(transform.position.x, transform.position.y, UpMoneyEffect.transform.position.z)-effectOffset;

        if (boosting){return;}
        if(Input.GetKeyDown(KeyCode.Space)){
            SwitchSide();
        }
    }
    void FixedUpdate()
    {
        if(!Started){return;}
        transform.Translate(transform.right * movingSpeed);
        eulerAngles = transform.eulerAngles;

        currentTrail.transform.position = transform.position;
        if(transform.position.y < 0){
            GameManager.GameOver();
        }
        if(boosting){return;}
        Collider2D overlap = Physics2D.OverlapCircle(transform.position, senseRadius);
        if(overlap!=null){
            if(overlap.name.Contains("option")){
                Debug.Log(overlap.name + " : " + overlap.name.LastIndexOf(","));
                float rate= float.Parse(overlap.name.Substring(overlap.name.LastIndexOf(",")+1));
                StartCoroutine(giveBoost(rate));

                Destroy(overlap.gameObject);
            }
        }
    }
    public static bool boosting =false;
    IEnumerator giveBoost(float perc){
        boosting=true;
        float newY = transform.position.y * perc;
        spriteRenderer.sprite = (perc > 1) ? upwardSprite : downwardSprite;
        // float boostingSpeed = transform.position.y * (1f/boostSpeed);
        float boostingSpeed = boostSpeed;
        if(transform.position.y < 250){boostingSpeed = boostSpeed/2f;}
        if(perc > 1){
            while(transform.position.y < newY){
                transform.position += new Vector3(0,boostingSpeed,0);
                
                if(!isUp){SwitchSide();}
                yield return new WaitForFixedUpdate();
            }
        }else{
            while(transform.position.y > newY){
                transform.position -= new Vector3(0,boostingSpeed,0);
                if(isUp){SwitchSide();}
                yield return new WaitForFixedUpdate();
            }
        }
        spriteRenderer.sprite = normalSprite;
        boosting=false;       
    }


    void SwitchSide(){
        if(!Started && !gameStarted){
            gameStarted = true;
            GameManager.instance.StartGame();
            Started=true;
        }

        transform.eulerAngles = new Vector3(transform.eulerAngles.x, transform.eulerAngles.y, isUp ? -turnAngle : turnAngle);

        GameObject newTrail = Instantiate(trailPrefab, transform.position, Quaternion.identity);
        currentTrail = newTrail.transform;
        newTrail.GetComponent<TrailRenderer>().startColor = newTrail.GetComponent<TrailRenderer>().endColor = (isUp) ? Color.green:Color.red;


        GetComponent<SpriteRenderer>().sprite = (isUp) ? upwardSprite : downwardSprite;
        if (isUp)
        {
            UpMoneyEffect.emissionRate = defeaultEmitRate;
        }
        else
        {
            UpMoneyEffect.emissionRate = 0;
        }

    }


    void OnDrawGizmos(){
        Gizmos.DrawWireSphere(transform.position, senseRadius);
    }
}
