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

public class PawnScript : Piece
{

    ChessScript cs;
    
    Quaternion origrot;
    Vector3 vec;

    bool jumppressed;
    float jumppressstarttime;

    bool walkdir;
    bool eatdir;

    bool nevermoved;

    float AIrotvel;
    int ddd;
    float AIturnstarted;

    public override void Awake()
    {

        base.Awake();
        nevermoved = true;
        //weorf = false;
        state = "inactive";
        GetComponent<Rigidbody>().centerOfMass = GetComponent<Rigidbody>().centerOfMass + new Vector3(0, -0.5f, 0);
        GetComponent<Rigidbody>().mass = 0.5f;
        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;
        jumppressed = false;
        cs.Intercepted();
        state = "aim";
        cs.wannarotation = Quaternion.Euler(0, transform.rotation.eulerAngles.y, 0);
    }

    public bool GoodDirection()
    {
        //print(transform.rotation.eulerAngles.y);
        bool cs1eatdir = transform.rotation.eulerAngles.y > 305 && transform.rotation.eulerAngles.y < 330 || transform.rotation.eulerAngles.y < 55 && transform.rotation.eulerAngles.y > 30;
        bool csm1eatdir = transform.rotation.eulerAngles.y > 125 && transform.rotation.eulerAngles.y < 150 || transform.rotation.eulerAngles.y > 215 && transform.rotation.eulerAngles.y < 235;
        if (cs.turn == 1)
            eatdir = cs1eatdir;
        else
            eatdir = csm1eatdir;
        if (power > 0) eatdir = cs1eatdir || csm1eatdir;
        bool cs1walkdir = transform.rotation.eulerAngles.y > 340 || transform.rotation.eulerAngles.y < 20;
        bool csm1walkdir = transform.rotation.eulerAngles.y > 160 && transform.rotation.eulerAngles.y < 200;
        if (cs.turn == 1)
            walkdir = cs1walkdir;
        else
            walkdir = csm1walkdir;
        if (power > 0) walkdir = cs1walkdir || csm1walkdir;
        if (walkdir) eatdir = false;
        // hits own
        LayerMask mask;
        if (cs.turn == 1)
            mask = 1 << LayerMask.NameToLayer("White");
        else
            mask = 1 << LayerMask.NameToLayer("Black");
        RaycastHit hitInfo;
        bool owncasthit = Physics.SphereCast(transform.position, 1.5f, transform.forward, out hitInfo, 4f, mask) ||
            Physics.Raycast(transform.position, transform.forward, out hitInfo, 4f, mask);
        //print(eatdir.ToString() + " " + walkdir.ToString() + " " + bigcasthit.ToString() + " " + smallcasthit.ToString());
        

        // hits other
        if (cs.turn == 1)
            mask = 1 << LayerMask.NameToLayer("Black");
        else
            mask = 1 << LayerMask.NameToLayer("White");
        bool bigcasthit = Physics.SphereCast(transform.position, 1.5f, transform.forward, out hitInfo, 6f, mask) ||
            Physics.Raycast(transform.position, transform.forward, out hitInfo, 6f, mask);
        //bool smallcasthit = Physics.SphereCast(transform.position, 1.5f, transform.forward, out hitInfo, 4f, mask);
        print("eat " + eatdir.ToString() + " walk " + walkdir.ToString() + " own " + owncasthit.ToString() + " big " + bigcasthit.ToString());// + " small " + smallcasthit.ToString());

        if (owncasthit) return false;
        return eatdir && bigcasthit || walkdir && !bigcasthit;

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

            if (!amAI)
            {
                string t = "Aim me with your mouse and make a move by clicking space.";
                if (nevermoved) t += "\nThis is your first move with me, so a long press makes a long jump.";
                instructions.text = t;
            }
            else
            {
                instructions.text = "The AI is thinking of moving this pawn.";
            }
            if (GoodDirection())
            {
                cs.CouldMove(true);
            }
            else
            {
                //print("false");
                cs.CouldMove(false);
            }
            //print("aim" + GetComponent<Rigidbody>().velocity.ToString());
            cs.wannaposition = transform.position + Vector3.up * 2.5f - transform.forward * 2.5f;
            //Read mouse input values.
            if (!amAI)
            {
                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);
                if (player == 1) // wanna go 0...
                {
                    if (transform.rotation.eulerAngles.y > 45 && transform.rotation.eulerAngles.y < 180) AIrotvel -= 0.1f;
                    if (transform.rotation.eulerAngles.y < 315 && transform.rotation.eulerAngles.y > 180) AIrotvel += 0.1f;
                }
                if (player == -1) // wanna go 0...
                {
                    if (transform.rotation.eulerAngles.y < 135) AIrotvel += 0.1f;
                    if (transform.rotation.eulerAngles.y > 225) AIrotvel -= 0.1f;
                }
                transform.Rotate(0, AIrotvel, 0);
                if (Mathf.Abs(AIrotvel) > 5) AIrotvel = AIrotvel / Mathf.Abs(AIrotvel) * 5;
            }

            //Look up and down only with your camera
            // cs.GetComponent<Camera>().transform.Rotate(Vector3.up, x*speed); // y * speed, x * speed, 0);
            cs.wannarotation = transform.rotation;
            cs.wannarotation = Quaternion.AngleAxis(20, transform.right) * cs.wannarotation;

            //CorrectDirections();

            if (!amAI && Input.GetKey(KeyCode.Escape) || amAI && Random.Range(0,200) == 0)
            {
                state = "inactive";
                cs.SendMessage("CancelMove");
            }
            if (!amAI && Input.GetKeyDown(KeyCode.Space))
            {
                jumppressed = true;
                jumppressstarttime = Time.time;
            }
            //print(ddd);
            //print("here");
            
            if (!amAI && Input.GetKeyUp(KeyCode.Space) || amAI && (Random.Range(0, ddd) == 0 || ddd == 1)) {
                //print("then");
                if (!amAI && Time.time - jumppressstarttime > 1)
                {
                    jumppressed = false;
                }
                else
                {
                    if (GoodDirection())
                    {
                        GetComponent<Rigidbody>().velocity = Vector3.zero;
                        //print(transform.forward * 500 + Vector3.up * 150);
                        transform.position = transform.position + Vector3.up * 0.1f;
                        float multiplier = 1; // + power*0.25f;
                        if ((Time.time - jumppressstarttime > 0.25f || amAI && Random.Range(0,2) == 0) &&
                            nevermoved && walkdir) multiplier = 1.4f;
                        GetComponent<Rigidbody>().AddForce(transform.forward * 215 * multiplier + Vector3.up * 75*multiplier);
                        cs.SendMessage("NextTurn");
                        state = "jumping";
                        nevermoved = false;
                    }
                }
            }
        }
        else if (state == "jumping")
        {
            if (GetComponent<Rigidbody>().velocity.magnitude < 0.2f)
            {
                //print("ok");
                cs.IHaveStopped(this);
                state = "inactive";
            }
        }
    }

}
