using Newtonsoft.Json.Converters;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class feedbackPageHandler : MonoBehaviour, IEventHandler
{
    // Start is called before the first frame update
    public string desvalue = "";
    public string emailvalue = "";
    private string[] upimgPath = { "", "", "", "" };
    void Start()
    {
        utilsTools.onEvent(this, "onFeedBackPageShow");
        utilsTools.HideView(gameObject);
        transform.Find("description/inputdata").GetComponent<TMPro.TMP_InputField>().onValueChanged.AddListener(onDescTextChange);
        transform.Find("description/inputdata").GetComponent<TMPro.TMP_InputField>().onEndEdit.AddListener(onDescTextEnd);
        transform.Find("contactInfo/inputdata").GetComponent<TMPro.TMP_InputField>().onValueChanged.AddListener(onMailBoxTextChange);
        transform.Find("contactInfo/inputdata").GetComponent<TMPro.TMP_InputField>().onEndEdit.AddListener(onMailBoxTextEnd);
    }

    // Update is called once per frame
    protected void Update()
    {
        var submit = transform.Find("submit").GetComponent<Button>();
        submit.interactable = false;
        int idx = getSelectTopic();
        if (idx < 0) return;
        if (desvalue.Equals("")) return;
        submit.interactable = true;
    }

    public void onAddImg()
    {

    }
    public void onDelImg()
    {

    }
    public void onBack()
    {
        utilsTools.HideView(gameObject);
    }
    public void onSubmit()
    {
        Dictionary<string, System.Object> updatas = new Dictionary<string, System.Object>();
        updatas.Add("desc", desvalue);
        updatas.Add("emali", emailvalue);
        updatas.Add("imgs", "null");
        updatas.Add("type", (getSelectTopic() + 1).ToString());
        string s = "dict{";
        foreach (var item in updatas)
        {
            s = s + item.Key + "=" + item.Value.ToString();
        }
        Debug.Log("datas = " + s);
        onBack();
    }
    public void onDescTextChange(string text)
    {
        var charsize = transform.Find("description/charsize").GetComponent<Text>();
        charsize.text = (1000 - text.Length).ToString() + "/1000";
        desvalue = text;
    }
    public void onDescTextEnd(string text)
    {
        desvalue = text;
        var charsize = transform.Find("description/charsize").GetComponent<Text>();
        charsize.text = (1000 - text.Length).ToString() + "/1000";
    }
    public void onMailBoxTextChange(string text)
    {
        emailvalue = text;
    }
    public void onMailBoxTextEnd(string text)
    {
        emailvalue = text;
    }
    public int getSelectTopic()
    {
        GameObject topic = transform.Find("topic").gameObject;
        int size = topic.transform.childCount;
        for(int i = 0;i < size; i++)
        {
            Toggle toggle = topic.transform.GetChild(i).GetComponent<Toggle>();
            if (toggle.isOn)
            {
                return i;
            }
        }
        return -1;
    }
    public void clearData()
    {
        desvalue = "";
        emailvalue = "";
        upimgPath[0] = "";
        upimgPath[1] = "";
        upimgPath[2] = "";
        upimgPath[3] = "";
        GameObject topic = transform.Find("topic").gameObject;
        int size = topic.transform.childCount;
        for (int i = 0; i < size; i++)
        {
            Toggle toggle = topic.transform.GetChild(i).GetComponent<Toggle>();
            toggle.isOn = false;
        }
        
        transform.Find("description/inputdata").GetComponent<TMPro.TMP_InputField>().text = "";
        transform.Find("contactInfo/inputdata").GetComponent<TMPro.TMP_InputField>().text = "";
    }

    public void onEventHandler(string eventName, List<Object> uobjects, List<object> objects)
    {
        if (eventName.Equals("onFeedBackPageShow"))
        {
            clearData();
            utilsTools.ShowView(gameObject);
        }
    }
}

