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

public class RouteCtr : MonoBehaviour
{
    /// <summary>
    /// 路径子物体
    /// </summary>
    public GameObject RouteItem;
    /// <summary>
    /// 所有路线
    /// </summary>
    private List<GameObject> list_route;
    [SerializeField]
    private CalMap m_CalMap;

    private Dictionary<int, List<Vector3>> m_RoutePoints;
    private List<Vector3> m_CurrentPoints;

    public List<Vector3> CurrentPoints
    {
        get { return m_CurrentPoints; }
    }

    private void Start()
    {
        list_route = new List<GameObject>();
        m_RoutePoints = new Dictionary<int, List<Vector3>>();
        m_CurrentPoints = new List<Vector3>();
    }


    public List<GameObject> List_route
    {
        get { return list_route; }
    }


    /// <summary>
    /// 设置路线数据
    /// </summary>
    /// <param name="navLines"></param>
    public void SetRouteValue(List<NavLinesItem> navLines)
    {
        m_RoutePoints.Clear();
        for (int i = 0; i < navLines.Count; i++)
        {
            List<Vector3> points = new List<Vector3>();
            SettingLuJing(navLines[i], navLines[i].routerMod - 1, out points);
            // 小地图显示路径
            m_CalMap.SetLines(navLines[i]);
            m_RoutePoints.Add(i, points);
        }
    }

    /// <summary>
    /// 设置路径
    /// </summary>
    /// <param name="data"></param>
    /// <param name="routerMod">线段样式</param>
    private void SettingLuJing(NavLinesItem data, int routerMod, out List<Vector3> points)
    {
        points = new List<Vector3>();
        GameObject routes = new GameObject(data.name);
        routes.transform.parent = transform;
        routes.transform.localPosition = Vector3.zero;
        routes.transform.localEulerAngles = Vector3.zero;
        for (int j = 0; j < data.lines.Count; j++)
        {
            var line = data.lines[j];
            GameObject route = GameObject.Instantiate(RouteItem, routes.transform);
            route.name = line._index.ToString();
            Vector3 startPos = new Vector3((float)line.x1 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, -(float)line.y1 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
            Vector3 endPos = new Vector3((float)line.x2 / GameManager.Instance.WebMapSize.x * GameManager.Instance.MapSize.x, 0, -(float)line.y2 / GameManager.Instance.WebMapSize.y * GameManager.Instance.MapSize.y);
            route.GetComponent<Route>().SetRoute(endPos, startPos, routerMod);
            route.SetActive(true);
            points.Add(startPos);
            if (j == data.lines.Count - 1)
            {
                points.Add(endPos);
            }
        }
        list_route.Add(routes);
        routes.SetActive(false);
    }

    /// <summary>
    /// 选择路线
    /// </summary>
    /// <param name="Index"></param>
    public void SettingLuJing(int Index, bool isshow)
    {
        if (list_route == null || list_route.Count <= 0)
        {
            Debug.LogError(" 未初始化路径 或 所选的路径不存在 ");
            return;
        }
        list_route[Index].SetActive(isshow);

        m_CurrentPoints = isshow ? m_RoutePoints[Index] : m_RoutePoints[0];

    }
}