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

public class PlayerController : MonoBehaviour
{
    public static PlayerController instance{get; private set;}
    public Sprite upwardSprite;
    public Sprite downwardSprite;
    public Sprite normalSprite;
    public static Transform t {get {return instance.transform; }}
    void Awake(){
        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;

    }
    public static bool Started;
    // Update is called once per frame
    void Update(){
        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(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;
        if(perc > 1){
            while(transform.position.y < newY){
                transform.position += new Vector3(0,boostSpeed,0);
                
                if(!isUp){SwitchSide();}
                yield return new WaitForFixedUpdate();
            }
        }else{
            while(transform.position.y > newY){
                transform.position -= new Vector3(0,boostSpeed,0);
                if(isUp){SwitchSide();}
                yield return new WaitForFixedUpdate();
            }
        }
        spriteRenderer.sprite = normalSprite;
        boosting=false;       
    }


    void SwitchSide(){
        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;

    }


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