using AX.InputSystem;
using AX.MessageSystem;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.UI;

/// <summary>
/// 车辆多选寻路
/// </summary>
public class MultiSelectNAV : ResourceLoadPanel<MultiSelectNAV>
{
    //public static MultiSelectNAV GetInstance;
    public Toggle MultiSelectNAVButton;
    public Toggle InstallNAVButton;
    public RectTransform Content;
    public GameObject MultiSelectNAVPanel;
    public GameObject Item;
    public List<GameObject> SelectCars;
    public List<Vector3> TargetPoint;
    public bool pathfindingFlag;

    void Start()
    {
        //GetInstance = this;
        MultiSelectNAVButton.onValueChanged.AddListener(ShowUI);
        InstallNAVButton.onValueChanged.AddListener(InstallNAV);
    }
    private void InstallNAV(bool Ison)
    {
        if (Ison)
        {
            TargetPoint.Clear();
            MessageDispatcher.AddListener("PATH_FINDING_COMMAND", PathFinding);
            CancelSelectedCommand.Instance.Execute(0, null);
        }
        else
            MessageDispatcher.RemoveListener("PATH_FINDING_COMMAND", PathFinding);
        pathfindingFlag = Ison;
        InputManager.MutiSelectFind = Ison;
        //if (!Ison) 
        //{
        //    if(!pathfindingFlag && TargetPoint.Count > 0)
        //    {
        //        //MultiSelectNavPath.instance.SetPathFind(TargetPoint, SelectCars);
        //        for (int i = 0; i < SelectCars.Count; i++)
        //        {
        //            SelectCars[i].GetComponent<AgentController>().MuitiNav(TargetPoint, i);
        //        }
        //    }
        //}
        if (!pathfindingFlag && TargetPoint.Count > 0)
        {
            var count = TargetPoint.Count;
            //最后目标点找到各车的停靠点
            var list = AgentController.LiSan(TargetPoint[count - 1], SelectCars.Count, new Vector3(9, 0, 9));
            int indexer = 0;
            //开始多选车辆的巡礼操作
            SelectCars.ForEach((X) =>
            {
                TargetPoint[count - 1] = list[indexer];
                X.GetComponent<AgentController>().AutoPathFinding(TargetPoint);
                indexer++;
            });
            //  TargetPoint.Clear();
        }
       
    }

    public void AddSelectCar(GameObject Car)
    {
        if (SelectCars.Contains(Car))
            return;
        SelectCars.Add(Car);
        var index = NAVSelectCarItem.SelectCarItem.FindIndex((X) => { return X.SelectCar == Car; });
        if (index >= 0)
        {
            NAVSelectCarItem.SelectCarItem[index].Toggle.isOn = true;
            return;
        }
        var item = Instantiate(Item, Content);
        var scipts = item.GetComponent<NAVSelectCarItem>();
        scipts.Text.text = Car.GetComponent<PowerAttribute>().Affiliation + "-" + Car.GetComponent<PowerAttribute>().Number;
        scipts.SelectCar = Car;
    }
    public void RemoveSelectCar(GameObject Car)
    {
        if (SelectCars.Contains(Car))
        {
            Car.GetComponent<AgentController>().Stop();
            SelectCars.Remove(Car);
        }
    }
    private void PathFinding(IMessage obj)
    {
        if (!pathfindingFlag)
            return;
        var data = (PathFindingCmdArgs)obj.Data;
        InputManager.pointerObj.transform.position = data.hitPoint;
        if (SelectCars.Count > 0)
        {
            for (int i = 0; i < SelectCars.Count; i++)
            {
                SelectCars[i].GetComponent<AgentController>().StopAllCoroutines();
            }
            bool haspath= SelectCars[0].GetComponent<AgentController>().CheckHasPath(data.hitPoint);
            if (haspath)
            {
                TargetPoint.Add(data.hitPoint);
            }
            else 
            {
                ResourceLoadWindow.Instance.LoadTextHintWindow("该点无法到达", 1f);
            }
        }
        else 
        {
            ResourceLoadWindow.Instance.LoadTextHintWindow("没有选中任何车辆", 1f);
            InstallNAVButton.isOn = false;
            return;
        }
    }

    private void ShowUI(bool Ison)
    {
        pathfindingFlag = false;
        SelectCars.Clear();
        TargetPoint.Clear();
        MultiSelectNAVPanel.SetActive(Ison);
        InstallNAVButton.gameObject.SetActive(Ison);
        InstallNAVButton.isOn = false;
        if (Ison)
            MessageDispatcher.AddListener("RADIO_SELECTED_COMMAND", RadioSelect);
        else
            MessageDispatcher.RemoveListener("RADIO_SELECTED_COMMAND", RadioSelect);
    }

    private void RadioSelect(IMessage obj)
    {
        var gameObjID = (long)obj.Sender;
        var hitObj = EntitiesManager.Instance.GetEntityByID(gameObjID);
        if (hitObj.gameObject.tag.Equals(GlobalVariable.CarTag))
        {
            if (SelectCars.Contains(hitObj))
                MessageDispatcher.SendMessage("RemoveSelectCar", hitObj);
            else
                AddSelectCar(hitObj);
        }
    }

    public void Clear()
    {
        MultiSelectNAVButton.isOn = false;
    }
    private void Update()
    {
        if (!pathfindingFlag && TargetPoint.Count > 0) 
        {
        
        }
    }
    private void OnDestroy()
    {
        MessageDispatcher.RemoveListener("PATH_FINDING_COMMAND", PathFinding);
        MessageDispatcher.RemoveListener("RADIO_SELECTED_COMMAND", RadioSelect);
        SelectCars.Clear();
        TargetPoint.Clear();
        NAVSelectCarItem.SelectCarItem.Clear();
    }
}