using SimpleJSON;
using System;
using System.Collections.Generic;
using System.Reflection;
using TMPro;
using UnityEngine;
using UnityEngine.UI;
using WS;


public class MainMenuController : Controller<MainMenuView, MainMenuModel>
{
    List<string> courses = new List<string>
    {
        "中药检索","中药学古文献","药物化学","药物化学","药物化学"
    };
    protected override void BindingViewModel()
    {
        ToggleGroupAddListener();
    }
    public override void OpenView()
    {
        base.OpenView();
    }
    void ToggleGroupAddListener()
    {
        Toggle[] toggles = _View.group.GetComponentsInChildren<Toggle>();
        for(int i=0; i < toggles.Length; i++)
        {
            var index = i;
            var rc = toggles[index].GetComponent<ReferenceCollector>();
            rc.GetComponent<Button>("EnterBtn").onClick.AddListener(OnEnterBtnClick);
            toggles[index].onValueChanged.AddListener((isOn) =>
            {
                rc.Get<GameObject>("OnChoice").SetActive(isOn);
                TextMeshProUGUI courseName = rc.GetComponent<TextMeshProUGUI>("CourseName_Normal");
                courseName.color = isOn ? new Color(240 / 255f, 206 / 255f, 119 / 255f, 255 / 255f) : Color.black;
                Vector2 size = isOn? new Vector2(430,1080):new Vector2(251,1080);
                toggles[index].GetComponent<RectTransform>().sizeDelta = size;
            });
        }
        toggles[0].isOn = true;
    }
    void OnEnterBtnClick()
    {
        SearchViewController controller = FacadeComponent.Instance.CreateController<SearchViewController>();
        controller.OpenView();
    }
}
public class MainMenuView : View
{
    public Transform group;
    List<string> courses = new List<string>
    {
        "中药检索","中药学古文献","药物化学","药物化学","药物化学"
    };
    public override void LoadInit()
    {
        var rc = UIGameObject.GetComponent<ReferenceCollector>();
        group = rc.GetComponent<Transform>("Content");
        InitCourseList();
    }
    public void InitCourseList()
    {
        var choiceItemPre = Resources.Load<GameObject>("UI/Common/Course");
        for (int i = 0; i < courses.Count; i++)
        {
            var choiceItem = GameObject.Instantiate(choiceItemPre, group);
            var rc = choiceItem.GetComponent<ReferenceCollector>();

            TextMeshProUGUI courseName = rc.GetComponent<TextMeshProUGUI>("CourseName_Normal");
            courseName.text = courses[i];
            Toggle toggle = choiceItem.GetComponent<Toggle>();
            toggle.group = group.GetComponent<ToggleGroup>();
        }
    }
}
public class MainMenuModel : Model
{
    public override void InitProperty()
    {
    }
}