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

public class HeadControl : MonoBehaviour
{
    public float tick = 1f;

    public bool realtimeExecute = true;
    public static List<HeadControl> headList = new List<HeadControl>();
    public TextMesh playerNamePrefab;

    [HideInInspector]
    //PhotonView pView;
    CommandList _commandObject;

    public CommandList commandObject
    {
        get
        {
            if (_commandObject == null)
            {
                _commandObject = gameObject.GetComponent<CommandList>();
            }

            return _commandObject;
        }
    }

    Movement _movementObject;
    public Movement movementObject
    {
        get
        {
            if (_movementObject == null)
            {
                _movementObject = gameObject.GetComponent<Movement>();
            }

            return _movementObject;
        }
    }

    Cannon _cannonObject;
    public Cannon cannonObject
    {
        get
        {
            if (_cannonObject == null)
            {
                _cannonObject = gameObject.GetComponent<Cannon>();
            }

            return _cannonObject;
        }
    }

    Rocket _rocketObject;
    public Rocket rocketObject
    {
        get
        {
            if (_rocketObject == null)
            {
                _rocketObject = gameObject.GetComponent<Rocket>();
            }

            return _rocketObject;
        }
    }


    MachineGun _machineGunObject;
    public MachineGun machineGunObject
    {
        get
        {
            if (_machineGunObject == null)
            {
                _machineGunObject = gameObject.GetComponent<MachineGun>();
            }

            return _machineGunObject;
        }
    }

    string currentCommandText = "";

    //void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
    //{
    //	if (stream.isWriting == true)
    //	{
    //		SendData( stream, info);
    //	}
    //	else
    //	{
    //		ReceivedData( stream, info);
    //	}
    //}
    //
    //void SendData(PhotonStream stream, PhotonMessageInfo info)
    //{
    //	stream.SendNext(currentCommandText);
    //}
    //
    //void ReceivedData(PhotonStream stream, PhotonMessageInfo info)
    //{
    //	currentCommandText = (string)stream.ReceiveNext();
    //}

    public void SetCommandText(string text)
    {
        currentCommandText = text;
    }

    List<CommandList.Command> realTimeQueue = new List<CommandList.Command>();

    public void AddCommand(CommandList.Command command)
    {
        realTimeQueue.Add(command);
    }

    // Use this for initialization
    void Start()
    {
        headList.Add(this);

        //pView = GetComponent<PhotonView>();
        //if (pView != null)
        //{
        //	if (pView.isMine == true)
        //	{
        //		CreatePlayerName("YOU");
        //	}
        //	else
        //	{
        //		CreatePlayerName("NOT YOU");
        //	}
        //}
    }

    void CreatePlayerName(string name = "")
    {
        TextMesh node = (TextMesh)Instantiate(playerNamePrefab, transform.position + (Vector3.down * 2), Quaternion.identity);
        node.transform.parent = transform;
        node.text = name;
    }

    void OnDestroy()
    {
        headList.Remove(this);
    }

    public bool HaveCommands()
    {
        return currentCommandText.Length != 0;
    }

    bool isExecute = false;
    //public void Execute()
    //{
    //	commandObject.SetCommandList(currentCommandText);
    //
    //	if (pView.isMine == true)
    //	{
    //		currentCommandText = "";
    //	}
    //
    //	isExecute = true;
    //}

    float waitingTime = 0;
    // Update is called once per frame
    void FixedUpdate()
    {
        if (realtimeExecute == true)
        {
            RealTimeUpdate();
        }
        else
        {
            NonRealtimeUpdate();
        }
    }

    void RealTimeUpdate()
    {
        foreach (CommandList.Command currentCommand in realTimeQueue)
        {
            DoCommand(currentCommand);
        }

        realTimeQueue.Clear();
    }

    void NonRealtimeUpdate()
    {
        if (isExecute == false)
        {
            return;
        }

        if (waitingTime <= 0)
        {
            waitingTime += tick;
            CommandList.Command currentCommand = commandObject.PopFirstCommand();
            DoCommand(currentCommand);
        }
        else
        {
            waitingTime -= Time.fixedDeltaTime;
        }
    }

    void DoCommand(CommandList.Command currentCommand)
    {
        if (currentCommand == CommandList.Command.MOVE_FORWARD && movementObject.backPower.GetPower() > 0)
        {
            movementObject.ChangeDirection(Movement.MoveDirection.MOVE_FORWARD);
        }
        else if (currentCommand == CommandList.Command.MOVE_BACKWARD && movementObject.frontPower.GetPower() > 0)
        {
            movementObject.ChangeDirection(Movement.MoveDirection.MOVE_BACKWARD);
        }
        else if (currentCommand == CommandList.Command.STOP_MOVING)
        {
            movementObject.ChangeDirection(Movement.MoveDirection.MOVE_STOP);
        }
        else if (currentCommand == CommandList.Command.FIRE_CANNON)
        {
            if (cannonObject.IsFinishCoolDown() && cannonObject.power.GetPower() > 0)
            {
                cannonObject.Fire();
            }
        }
        else if (currentCommand == CommandList.Command.FIRE_ROCKET)
        {
            if (rocketObject.IsFinishCoolDown() && rocketObject.power.GetPower() > 0)
            {
                rocketObject.Fire();
            }
        }
        else if (currentCommand == CommandList.Command.FIRE_MACHINEGUN)
        {
            if (machineGunObject.IsFinishCoolDown() && machineGunObject.power.GetPower() > 0)
            {
                machineGunObject.Fire();
            }
        }
        else if (currentCommand == CommandList.Command.NONE)
        {
            movementObject.ChangeDirection(Movement.MoveDirection.MOVE_STOP);
            isExecute = false;
        }
    }

    public bool IsFinishExecute()
    {
        return isExecute;
    }
}
