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

public class MovePlayer : MonoBehaviour
{
    public float xpos, zpos;
    public float force_P1, force_P2, cForce, maxForce, minForce;
    public Rigidbody rb;
    public GameObject enemy;
    public Vector3 v3_P1, v3_P2;
    private bool isAddForce_P1, isAddForce_P2, isBackCenter_P1, isBackCenter_P2;
    private bool isGameStart;
    public GameObject mainGame;


    // Start is called before the first frame update
    void Start()
    {
        cForce = 100;
        maxForce = 600;
        isAddForce_P1 = false;
        isAddForce_P2 = false;
        isBackCenter_P1 = false;
        isBackCenter_P2 = false;
        isGameStart = false;
    }



    // Update is called once per frame
    void Update()
    {
        
    }
    public void AddForceToPlayer()
    {
        float xForce, zForce;
        //float zpos_P1, zpos_P2;
        float deltaX, deltaZ;
        float xDir, zDir;
        //float force;

        if (isGameStart)
        {


            if (this.name == "Player1" && !isAddForce_P1)
            {
                isAddForce_P1 = true;
                force_P1 = SpinnerMainGame.instance.GetForce("P1");
                //Debug.Log(force_P1);
                enemy = GameObject.Find("Player2");
                v3_P1 = this.transform.position;
                v3_P2 = enemy.transform.position;
                //Debug.Log(v3_P2);
                deltaX = v3_P2.x - v3_P1.x;
                if (deltaX >= 0)
                {
                    xDir = 1;
                }
                else
                {
                    xDir = -1;
                }
                deltaZ = v3_P2.z - v3_P1.z;
                if (deltaZ > 0)
                {
                    zDir = 1;
                }
                else
                {
                    zDir = -1;
                }
                if (deltaZ > -0.1f && deltaZ < 0.1f)
                {
                    zForce = 0;
                    xForce = force_P1;
                }
                else
                {
                    zForce = force_P1 / Mathf.Sqrt(deltaX * deltaX / deltaZ / deltaZ + 1);
                    if (zForce > maxForce) zForce = maxForce;
                    xForce = zForce * Mathf.Abs(deltaX / deltaZ);
                    if (xForce > maxForce) xForce = maxForce;
                }


                rb.AddForce(xForce * xDir, 0, zForce * zDir);

            }
            if (this.name == "Player2" && !isAddForce_P2)
            {
                isAddForce_P2 = true;
                force_P2 = SpinnerMainGame.instance.GetForce("P2");
                enemy = GameObject.Find("Player1");
                v3_P2 = this.transform.position;
                v3_P1 = enemy.transform.position;

                deltaX = v3_P1.x - v3_P2.x;
                if (deltaX >= 0)
                {
                    xDir = 1;
                }
                else
                {
                    xDir = -1;
                }
                deltaZ = v3_P1.z - v3_P2.z;
                if (deltaZ > 0)
                {
                    zDir = 1;
                }
                else
                {
                    zDir = -1;
                }
                if (deltaZ > -0.1f && deltaZ < 0.1f)
                {
                    zForce = 0;
                    xForce = force_P2;
                }
                else
                {
                    zForce = force_P2 / Mathf.Sqrt(deltaX * deltaX / deltaZ / deltaZ + 1);
                    if (zForce > maxForce) zForce = maxForce;
                    xForce = zForce * Mathf.Abs(deltaX / deltaZ);
                    if (xForce > maxForce) xForce = maxForce;
                }
                //Debug.Log(this.name + "," + xForce + "," + zForce);
                rb.AddForce(xForce * xDir, 0, zForce * zDir);

            }
        }
    }
    public void ReturnToCenter()
    {
        float deltaX, deltaZ;
        float xDir,zDir;
        float xForce, zForce;
        Vector3 v3;

        if (isGameStart)
        {


            if (this.name == "Player1" && !isBackCenter_P1)
            {
                isBackCenter_P1 = true;
                v3 = this.transform.position;
                deltaX = 0 - v3.x;
                if (deltaX >= 0)
                {
                    xDir = 1;
                }
                else
                {
                    xDir = -1;
                }
                deltaZ = 0 - v3.z;
                if (deltaZ > 0)
                {
                    zDir = 1;
                }
                else
                {
                    zDir = -1;
                }
                if (deltaZ > -0.1f && deltaZ < 0.1f)
                {
                    zForce = 0;
                    xForce = cForce;
                }
                else
                {
                    zForce = cForce / Mathf.Sqrt(deltaX * deltaX / deltaZ / deltaZ + 1);
                    
                    if (zForce > maxForce) zForce = maxForce;
                    xForce = zForce * Mathf.Abs(deltaX / deltaZ);
                    if (xForce > maxForce) xForce = maxForce;
                }
                //Debug.Log(deltaX + "," + deltaZ);
                //Debug.Log(xForce + "," + zForce);
                rb.AddForce(xForce * xDir, 0, zForce * zDir);
                isBackCenter_P1 = false;
            }

            if (this.name == "Player2" && !isBackCenter_P2)
            {
                isBackCenter_P2 = true;
                v3 = this.transform.position;
                deltaX = 0 - v3.x;
                if (deltaX >= 0)
                {
                    xDir = 1;
                }
                else
                {
                    xDir = -1;
                }
                deltaZ = 0 - v3.z;
                if (deltaZ > 0)
                {
                    zDir = 1;
                }
                else
                {
                    zDir = -1;
                }
                if (deltaZ > -0.1f && deltaZ < 0.1f)
                {
                    zForce = 0;
                    xForce = cForce;
                }
                else
                {
                    zForce = cForce / Mathf.Sqrt(deltaX * deltaX / deltaZ / deltaZ + 1);
                    //Debug.Log(deltaX * deltaX / deltaZ / deltaZ + 1);
                    if (zForce > maxForce) zForce = maxForce;
                    xForce = zForce * Mathf.Abs(deltaX / deltaZ);
                    if (xForce > maxForce) xForce = maxForce;
                }
                //Debug.Log(deltaX + "," + deltaZ);
                //Debug.Log(xForce + "," + zForce);
                rb.AddForce(xForce * xDir, 0, zForce * zDir);
                isBackCenter_P2 = false;
            }

        }
    }
    public void ResetAddForce(string player)
    {
        if(player == "P1")
        {
            isAddForce_P1 = false;
        }
        if(player == "P2")
        {
            isAddForce_P2 = false;
        }
            
        
    }
    public void GameStatus(string status)
    {
        if(status == "Start")
        {
            isGameStart = true;
        }
        if(status == "Over")
        {
            isGameStart = false;
        }
    }
    private void OnTriggerEnter(Collider other)
    {
        
        if(other.name=="Base Collider")
        {
            if(this.name == "Player1")
            {
                Debug.Log(other.name + this.name);
                mainGame.SendMessage("SetPlayerLose", "P1");
            }
            if (this.name == "Player2")
            {
                Debug.Log(other.name + this.name);
                mainGame.SendMessage("SetPlayerLose", "P2");
            }
        }
    }
}
