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

public class KingScript : Piece
{
    ChessScript cs;

    Quaternion origrot;
    Vector3 vec;
    string jumptype;

    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.8f;
        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;
        jumptype = "cardinal";
        if (y < thres || y > 360 - thres) return true;
        if (y > 90 - thres && y < 90 + thres) return true;
        if (y > 180 - thres && y < 180 + thres) return true;
        if (y > 270 - thres && y < 270 + thres) return true;
        jumptype = "diagonal";
        if (y > 45 - thres && y < 45 + thres) return true;
        if (y > 135 - thres && y < 135 + thres) return true;
        if (y > 225 - thres && y < 225 + thres) return true;
        if (y > 315 - thres && y < 315 + thres) return true;
        return false;
    }
    public override void Update()
    {
        base.Update();
        if (cs.state == "checkmate") return;
        if (state == "aim")
        {
            if (!amAI)
                instructions.text = "Why does my wife get all the attention?";
            else
                instructions.text = "The AI is doing the king.";
            if (GoodDirection())
            {
                cs.CouldMove(true);
            }
            else
            {
                cs.CouldMove(false);
            }
            cs.wannaposition = transform.position + Vector3.up * 3f - transform.forward * 2.5f;  //transform.position;
            //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);
                transform.Rotate(0, AIrotvel, 0);
                if (Mathf.Abs(AIrotvel) > 20) AIrotvel = AIrotvel / Mathf.Abs(AIrotvel) * 10;
            }

            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.GetKeyUp(KeyCode.Space) || amAI && (Random.Range(0, ddd) == 0 || ddd == 1))
            {
                if (GoodDirection())
                {
                    bool AIcancel = false;
                    if (amAI)
                    {
                        LayerMask ownmask;
                        if (player == 1)
                            ownmask = 1 << LayerMask.NameToLayer("White");
                        else
                            ownmask = 1 << LayerMask.NameToLayer("Black");
                        RaycastHit hitInfo2;
                        bool owncasthit = Physics.SphereCast(transform.position, 1.5f, transform.forward, out hitInfo2, 5f, ownmask) ||
                            Physics.Raycast(transform.position, transform.forward, out hitInfo2, 5f, ownmask);
                        if (owncasthit) AIcancel = true;
                    }
                    if (!AIcancel)
                    {
                        GetComponent<Rigidbody>().velocity = Vector3.zero;
                        //print(transform.forward * 500 + Vector3.up * 150);
                        transform.position = transform.position + Vector3.up * 0.1f;
                        if (jumptype == "cardinal")
                            GetComponent<Rigidbody>().AddForce(transform.forward * (270 / (power + 1)) + Vector3.up * 205);
                        if (jumptype == "diagonal")
                            GetComponent<Rigidbody>().AddForce(transform.forward * (350 / (power + 1)) + Vector3.up * 255);
                        cs.SendMessage("NextTurn");
                        state = "jumping";
                    }
                }
            }
        }
        else if (state == "jumping")
        {
            if (GetComponent<Rigidbody>().velocity.magnitude < 0.2f)
            {
                cs.IHaveStopped(this);
                state = "inactive";
            }
            else
            {
                //print(GetComponent<Rigidbody>().velocity)
                ;
            }
        }
    }
    public override void InformHit(Collider c)
    {
        //print("I hit am " + player.ToString());
        int otherplayer = c.transform.GetComponent<Piece>().player;
        if (true) // && (GetComponent<Rigidbody>().velocity.magnitude < collision.rigidbody.velocity.magnitude))
        {
            Vector3 diff = c.transform.position - transform.position;
            float forwo = Vector3.Dot(diff, transform.forward);
            //diff = (diff - transform.forward * forwo).normalized;

            diff = new Vector3(diff.x, 0, diff.z);

            c.GetComponent<Rigidbody>().velocity = Vector3.zero;
            c.transform.position = c.transform.position + Vector3.up * 0.2f;

            c.GetComponent<Rigidbody>().AddForce(diff * 500 * 1 + Vector3.up * 300 * 1); //AddExplosionForce(1000, transform.position, 50);

            GetComponent<Rigidbody>().velocity = GetComponent<Rigidbody>().velocity / 1.5f;

            // StopJump();
        }
    }

}
