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

public class PonyScript : Piece
{
    ChessScript cs;

    Quaternion origrot;
    Vector3 vec;

    float AIrotvel;
    int ddd;
    float AIturnstarted;

    public override void Awake()
    {
        base.Awake();
        //weorf = false;
        state = "inactive";
        GetComponent<Rigidbody>().centerOfMass = GetComponent<Rigidbody>().centerOfMass + new Vector3(0, -1f, 0);
        GetComponent<Rigidbody>().mass = 0.6f;
        cs = GameObject.Find("Camera").GetComponent<ChessScript>();
        //timetotakewoirj = Time.time;
    }

    // Update is called once per frame
    void Clicked()
    {
        ddd = 20;
        AIturnstarted = Time.time;
        if (state != "inactive") return;
        if (cs.immediateturn != player) return;
        cs.Intercepted();
        state = "aim";
        cs.wannarotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    }

    public bool GoodDirection()
    {
        //print(transform.rotation.eulerAngles.y);
        float y = transform.rotation.eulerAngles.y;
        float thres = 9f;
        if (y > 30 - thres && y < 30 + thres) return true;
        if (y > 60 - thres && y < 60 + thres) return true;
        if (y > 120 - thres && y < 120 + thres) return true;
        if (y > 150 - thres && y < 150 + thres) return true;
        if (y > 210 - thres && y < 210 + thres) return true;
        if (y > 240 - thres && y < 240 + thres) return true;
        if (y > 300 - thres && y < 300 + thres) return true;
        if (y > 330 - thres && y < 330 + thres) return true;
        return false;
    }

    public void CreateCopy()
    {
        string obje = GetComponent<Piece>().player == 1 ? "WhitePony" : "BlackPony";
        GameObject q = GameObject.Instantiate(Resources.Load<GameObject>("Pieces/" + obje));
        q.transform.position = transform.position + Vector3.up*3;
        q.transform.rotation = transform.rotation;
    }

    public override void Update()
    {
        base.Update();
        if (cs.state == "checkmate") return;
        if (power > 0)
        {
            power -= 1;
            CreateCopy();
        }
        if (state == "aim")
        {
            if (amAI && Time.time - AIturnstarted > 2) ddd = 5;

            if (!amAI)
            {
                instructions.text = "Aim me with your mouse and make me jump by clicking space. Nnnneighhh!!";
            }
            else
            {
                instructions.text = "The AI is thinking about ponies.";
            }

            if (GoodDirection())
            {
                cs.CouldMove(true);
            }
            else
            {
                //print("false");
                cs.CouldMove(false);
            }
            cs.wannaposition = transform.position + Vector3.up * 3f - transform.forward * 2.5f;
            if (!amAI)
            {
                //Read mouse input values.
                float x = Input.GetAxis("Mouse X");
                float y = Input.GetAxis("Mouse Y");

                float speed = 3f;
                //Rotate the player right and left
                transform.Rotate(0, x * speed, 0);
            }
            else
            {

                AIrotvel += Random.Range(-0.5f, 0.5f);
                transform.Rotate(0, AIrotvel, 0);
                if (Mathf.Abs(AIrotvel) > 15) AIrotvel = AIrotvel / Mathf.Abs(AIrotvel) * 15;
            }

            cs.wannarotation = transform.rotation;
            cs.wannarotation = Quaternion.AngleAxis(20, transform.right) * cs.wannarotation;

            if (!amAI && Input.GetKey(KeyCode.Escape) || amAI && Random.Range(0, 200) == 0)
            {
                state = "inactive";
                cs.SendMessage("CancelMove");
            }
            if (ddd == 1 && GoodDirection())
            {
                print(Random.Range(0, ddd) == 0);
                //GoodDirection();
                print("yupsss");
                
            }
            if (!amAI && Input.GetKey(KeyCode.Space) || amAI && Random.Range(0, ddd) == 0)
            {
                if (GoodDirection())
                {
                    bool AIcancel = false;
                    if (amAI)
                    {
                        LayerMask mask;
                        mask = 1 << LayerMask.NameToLayer("Default");
                        RaycastHit hitInfo;
                        float len = 4 * Mathf.Sqrt(5f);
                        bool boardhit = Physics.Raycast(transform.position + transform.forward * len + Vector3.down * 4,
                            Vector3.up, out hitInfo, 8, mask);
                        if (!boardhit) AIcancel = true;
                    }
                    GetComponent<Rigidbody>().velocity = Vector3.zero;
                    //print(transform.forward * 500 + Vector3.up * 150);
                    transform.position = transform.position + Vector3.up * 0.1f;
                    GetComponent<Rigidbody>().AddForce(transform.forward * 210 + Vector3.up * 355);
                    cs.SendMessage("NextTurn");
                    state = "jumping";
                }
            }
        }
        else if (state == "jumping")
        {
            if (GetComponent<Rigidbody>().velocity.magnitude < 0.2f)
            {
                cs.IHaveStopped(this);
                state = "inactive";
            }
        }
    }
}
