using Assets.Cyp.Common;
using Assets.Scripts;
using Assets.Scripts.Apis.Models;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEditor;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;

public class MapList : MonoBehaviour
{
    // Start is called before the first frame update
    [SerializeField] GameObject map;
    [SerializeField] GameObject content;
    [SerializeField] Dropdown hardSelector;
    [HideInInspector] public int pageIndex = 0;
    [HideInInspector] public int pageSize = 20;
    [HideInInspector] public string hard = "全部";
    public GameObject Content {
        get 
        {
            return content;
        }
    }
    private bool isEnd = false;
    void Start() 
    {
        if (hardSelector != null) 
        {
            hardSelector.onValueChanged.AddListener(ChangeHard);
        }
        if (content!=null)
        {
            var transform = content.transform.GetComponent<RectTransform>();
            var layout = content.GetComponent<GridLayoutGroup>();
            if (layout != null) 
            {
                var width = transform.rect.width;
                layout.cellSize = new Vector2((width - 120) / 5, (width - 120) / 5);
            }
        }
        GetList();
        
        //var obj = Instantiate(Resources.Load<GameObject>("Canvas"));
        //obj.transform.parent = transform.parent;
    }

    private void ChangeHard(int index)
    {
        var text = hardSelector.options[index].text;
        if (text == "全部难度") 
        {
            
        }
    }

    public void GetList() 
    {
        if (isEnd) return;
        var res = Global.mapApi.GetList(pageIndex, pageSize, "");
        if (res.result)
        {
            
            if (res.data.Count == 0)
            {
                isEnd = true;
            }
            else 
            {
                DisplayMaps(res.data);
            }
        }
        
    }
    void DisplayMaps(List<MapRoute> list) 
    {
        if (map != null)
        {
            foreach (var item in list)
            {
                var obj = Instantiate(map);
                obj.GetComponent<MapScript>().Initial(item);
                //obj.SendMessage("Initial", );
                obj.transform.parent = content.transform;
                obj.transform.localScale = new Vector3(1, 1, 1);
            }
        }
    }
    public void Refresh() 
    {
        content.transform.DestroyChildren();
        pageIndex = 0;
        GetList();
    }
    // Update is called once per frame
    void Update()
    {
        
    }
}
