using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;

public class PlayerController : MonoBehaviour
{

    void Awake(){
        instance = this;
    }

    public static PlayerController instance;
    public static Transform t => instance.transform ?? Camera.main.transform;
    public static Vector3 position=> (instance == null) ? Camera.main.transform.position : instance.transform.position;
    public float movingSpeed = 1;
    public float speedIncremental = 0.01f;
    public float verticalSpeed = 0.5f;
    public float rotationRange = 15;
    public bool invert= true;

    public Text txtScore;

    public GameObject ExplosionFX;

    void Start()
    {
        
    }

    // float t2=0;
    // Update is called once per frame
    void FixedUpdate()
    {
        // if(t2 < 1){
        //     t2+=Time.deltaTime;
        //     return;
        // }
        transform.Translate(new Vector3(movingSpeed,0), Space.World);

        input = Mathf.Lerp(input, dif / inputRange, inputSmoothness);

        transform.Translate(new Vector3(0,input * verticalSpeed), Space.World);
        transform.localEulerAngles = new Vector3(0,0,input * rotationRange);

        txtScore.text = (transform.position.x < 0) ? "" : transform.position.x.ToString("n0");

        movingSpeed += speedIncremental * Time.deltaTime;

    }
    public float input = 0;
    public float inputSmoothness =0.1f;
    public float inputRange = 200;
    public float dif = 0;
    float startedPos=0;

    public void OnPointerDown(BaseEventData e){
        PointerEventData ped = (PointerEventData) e as PointerEventData;

        startedPos = ped.position.y;
    }

    public void OnPointerDrag(BaseEventData e){
        PointerEventData ped = (PointerEventData) e as PointerEventData;
        
        dif = (invert) ? ped.position.y-startedPos: startedPos - ped.position.y;
    }

    public void OnPointerUp(BaseEventData e){
        dif = 0;
        startedPos = 0;
    }


    public void GameOver(){
        // Application.LoadLevel(0);
        GameOverUI.Activate();

        Instantiate(ExplosionFX, transform.position, Quaternion.identity);
        Destroy(this);
    }
}
