using NPOI.SS.Formula.Functions;
using ScrollViewUI;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using WS;
using static MedicineAllConfig;




public class SearchTypeEntryController : Controller<SearchTypeEntryView, SearchTypeEntryModel>
{
    public override void Dispose()
    {
        base.Dispose();
    }

    protected override void BindingViewModel()
    {
        UpdateUIData();
        this._Model.Bind(this._View.btnBack, () =>
        {
            Debug.Log("返回主页面");

            FacadeComponent.Instance.CreateController<SearchTypeEntryController>().OpenView();

            FacadeComponent.Instance.CloseView(this._View, null);
        });

        this._Model.Bind(this._View.ifMedcineSeach, this._Model.txtMedcineSeach, (str) =>
        {
           
            bool isShowMedcine = string.IsNullOrEmpty(str);
            this._View.ifMedcineSeach.text = str;
            this._View.rtMedcineAllParent.gameObject.SetActive(isShowMedcine);
            this._View.rtSeachParent.gameObject.SetActive(!isShowMedcine);
            if (isShowMedcine)
            {
                UpdateScrollViewMeadicineAll();

            }
            else
            {
                UpdateScrollViewSeach();
            }

        });


        this._Model.txtMedcineSeach.Value = "";

    }
    public void UpdateUIData()
    {
        var arr = this._Model.mdAllconfig.dicPin;
        if (this._Model.dicIndexId == null)
        {

            this._Model.dicIndexId = new List<int>();
            var dicIndexId = this._Model.dicIndexId;

            int rowLen = this._View.scvMedcineAll.fixedCount;
            for (int i = 0; i < arr.Count; i++)
            {


                //每个字母元素的 尾部 填充
                int addCount = rowLen - dicIndexId.Count % rowLen;
                addCount = addCount == rowLen ? 0 : addCount;
                for (int k = 0; k < addCount; k++)
                {

                    dicIndexId.Add(0);

                }







                if (arr[i].Count > 0)
                {
                    //字母元素的 前部填充
                    dicIndexId.Add(-(65 + i));

                    for (int j = 1; j < rowLen; j++)
                    {

                        dicIndexId.Add(0);

                    }

                    //添加里面的元素
                    for (int j = 0; j < arr[i].Count; j++)
                    {
                        //加一排


                        dicIndexId.Add((int)arr[i][j]);



                    }

                }









            }
        }


    }
    public void UpdateScrollViewMeadicineAll()
    {

        //update data
        var arrPin = this._Model.mdAllconfig.dicPin;
        //默认选中的 
        this._View.curClickGO.id = arrPin[0][0];




        //updata ui

        UpdateChoose();
        var count = this._Model.dicIndexId.Count;
        this._View.scvMedcineAll.SetData(count, (idx, go) =>
        {
            int id = this._Model.dicIndexId[idx];
            go.SetActive(id != 0);
            if (id == 0) return;

            var txt = go.GetComponentInChildren<TextMeshProUGUI>();
            var img = go.GetComponent<Image>();
            var btn = go.GetComponent<Button>();

            if (id < 0)
            {
                txt.text = (char)Mathf.Abs(id) + ":";

                btn.enabled = false;
                img.enabled = false;
            }
            else
            {
                btn.enabled = true;
                img.enabled = true;
                var ele = this._Model.mdAllconfig.Get(id);



                txt.text = ele.name;

                img.color = Color.yellow;


                if (this._View.curClickGO.id == ele.id)
                {
                    img.color = Color.red;
                    this._View.curClickGO.img = img;

                }

                btn.onClick.RemoveAllListeners();
                btn.onClick.AddListener(() =>
                {
                    if (this._View.curClickGO.img != null)
                    {
                        this._View.curClickGO.img.color = Color.yellow;
                    }
                    this._View.curClickGO.id = ele.id;
                    this._View.curClickGO.img = img;
                    img.color = Color.red;
                    UpdateChoose();
                });
            }







        });


    }



    public void UpdateScrollViewSeach()
    {

        //默认选中的 
        this._View.curClickGO.id = this._Model.listSeach[0].id;

        this._View.scvSeach.SetData(this._Model.listSeach.Count, (idx, go) =>
        {
            var txt = go.GetComponentInChildren<TextMeshProUGUI>();
            var img = go.GetComponent<Image>();
            var btn = go.GetComponent<Button>();


            var ele = this._Model.listSeach[idx];



            txt.text = ele.name;

            img.color = Color.yellow;


            if (this._View.curClickGO.id == ele.id)
            {
                img.color = Color.red;
                this._View.curClickGO.img = img;

            }

            btn.onClick.RemoveAllListeners();
            btn.onClick.AddListener(() =>
            {
                if (this._View.curClickGO.img != null)
                {
                    this._View.curClickGO.img.color = Color.yellow;
                }
                this._View.curClickGO.id = ele.id;
                this._View.curClickGO.img = img;
                img.color = Color.red;
                UpdateChoose();
            });
        });


    }

    public void UpdateChoose()
    {
        int id = this._View.curClickGO.id;
        var ele = this._Model.mdAllconfig.Get(id);

        this._View.txtMedcineName.text = ele.name;

        this._View.txtMedcineDes.text = ele.characteristic;

    }
}

public class SearchTypeEntryModel : Model
{
    public MedicineAllConfig mdAllconfig;
    public List<MedicineEle> listSeach;
    public List<int> dicIndexId;

    public BindableProperty<string> txtMedcineSeach;

    public override void InitProperty()
    {
        txtMedcineSeach = new BindableProperty<string>();
        listSeach = new List<MedicineEle>();

        mdAllconfig = Resources.Load<MedicineAllConfig>("Config/Excel/MedicineAllConfig");


        mdAllconfig.dicPin = new List<List<int>>();
        for (int i = 0; i < 26; i++)
        {
            mdAllconfig.dicPin.Add(new List<int>());
        }
        int pin = 0;
        foreach (var item in mdAllconfig.list)
        {
            pin = (int)item.pinType - 65;
            mdAllconfig.dicPin[pin].Add(item.id);
        }



    }
}
public class CurClickGO
{
    public int id;
    public Image img;
}

public class SearchTypeEntryView : View
{
    public GameObject scrollGo;

    public TMPro.TMP_InputField ifMedcineSeach;
    public RectTransform rtSeachParent;
    public RectTransform rtMedcineAllParent;
    public ScrollCycleView scvSeach;
    public ScrollCycleView scvMedcineAll;
    public TMPro.TextMeshProUGUI txtMedcineDes;
    public TMPro.TextMeshProUGUI txtMedcineName;
    public TMPro.TextMeshProUGUI txtTitle;
    public Button btnEnterClass;
    public Button btnBack;

    public CurClickGO curClickGO = new CurClickGO();



    public override void LoadInit()
    {

        ShowModeType = ViewShowModeType.Screen;
        ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();

        ifMedcineSeach = rc.GetComponent<TMP_InputField>("ifMedcineSeach");

        var canvas = GameObject.Find("Init/UI/SceneCanvas");


        rtSeachParent = rc.GetComponent<RectTransform>("rtSeachParent");

        scvSeach = new ScrollCycleView(canvas.transform, this.rtSeachParent, ScrollCycleView.DragType.Vertical);
        scvSeach.OnInit();
        //





        rtMedcineAllParent = rc.GetComponent<RectTransform>("rtMedcineAllParent");
        scvMedcineAll = new ScrollCycleView(canvas.transform, this.rtMedcineAllParent, ScrollCycleView.DragType.Vertical);
        scvMedcineAll.OnInit();







        txtMedcineName = rc.GetComponent<TextMeshProUGUI>("txtMedcineName");
        txtMedcineDes = rc.GetComponent<TextMeshProUGUI>("txtMedcineDes");
        txtTitle = rc.GetComponent<TextMeshProUGUI>("txtTitle");
        btnEnterClass = rc.GetComponent<Button>("btnEnterClass");
        btnBack = rc.GetComponent<Button>("btnBack");




    }
}
