using Newtonsoft.Json.Linq;
using System;
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.UI;

public class SearchCtrl : MonoBehaviour
{
    [SerializeField] GameObject opsion;

    [SerializeField] TMP_InputField name;
    [SerializeField] TMP_Dropdown suitStyle;
    [SerializeField] TMP_Dropdown suitType;
    [SerializeField] TMP_Dropdown region;
    [SerializeField] TMP_Dropdown animalTypes;
    [SerializeField] Toggle[] colors;
    [SerializeField] Sprite[] isOnSprite;

    private SearchRequest request;
    private List<Library> librarys;

    private void Awake()
    {
        opsion.SetActive(false);
    }

    public void OpsionButton()
    {
        opsion.SetActive(!opsion.activeSelf);
        ResetButton();
    }

    public void SearchButton()
    {
        Debug.Log("검색 시작");
        request = new SearchRequest();
        request.fursuitName = name.text;
        if (suitStyle.value != 0)
            request.suitStyle = GameManager.Instance.suitStyle[suitStyle.value - 1].code_id;
        if (suitType.value != 0)
            request.suitType = GameManager.Instance.suitType[suitType.value - 1].code_id;
        if (region.value != 0)
            request.suitRegion = GameManager.Instance.region[region.value - 1].code_id;
        if (animalTypes.value != 0)
            request.suitAnimalType = GameManager.Instance.animalTypes[animalTypes.value - 1].code_id;
        NetworkManager.Instance.CreateRequest<SearchResponse>("library/select/suiters", request, (data) =>
        {
            librarys = data.data;
            for(int n = 0; n < data.data.Count; n++)
            {
                Debug.Log($"name : {data.data[n].suit_name}");
            }
        });
    }

    public void NextSearch()
    {
        request.page++;
        NetworkManager.Instance.CreateRequest<SearchResponse>("library/select/suiters", request, (data) =>
        {
            librarys.AddRange(data.data);
            for (int n = 0; n < data.data.Count; n++)
            {
                Debug.Log($"name : {data.data[n].suit_name}");
            }
        });
    }

    public void GetImage(int velue)
    {
        NetworkManager.Instance.CreateImageRequest(librarys[velue].imageBase64.Substring(1), new object(), (data) =>
        {
            librarys[velue]._suit_image = Sprite.Create(data, new Rect(0, 0, data.width, data.height), new Vector2(0.5f, 0.5f));
        }, null, BestHTTP.HTTPMethods.Get);
    }

    public void ResetButton()
    {
        suitStyle.value = 0;
        suitType.value = 0;
        region.value = 0;
        animalTypes.value = 0;
        for(int n = 0; n < colors.Length; n++)
        {
            colors[n].isOn = false;
            colors[n].image.sprite = isOnSprite[0];
        }
    }

    public void OnToggle(int velue)
    {
        colors[velue].image.sprite = isOnSprite[(colors[velue].isOn ? 1 : 0)];
    }
}

public class SearchRequest
{
    public int xid;
    public string fursuitName = ""; /*오너,슈트 이름*/
    public string suitMaker; /*사용안함*/
    public string suitRegion = ""; /*활동 국가*/
    public string suitStyle = ""; /*슈트 스타일*/
    public string suitAnimalType = ""; /*슈트 동물 종*/
    public string suitType = ""; /*슈트 형태*/
    public List<string> colorList = new List<string>(); /*슈트 색상*/
    public int page = 1;
    public int size = 5;
    public int offset = 0;
}
public class SearchResponse
{
    public List<Library> data;
    public bool hasMore;
}
public class ImageResponse
{
    public byte[] imageBytes;
}

public class Library
{
    public string imageBase64;
    public string suit_name;
    public string owner_id;
    public string owner_name;
    public string production_date;
    public string suit_image;//사용안함
    public Sprite _suit_image;
    public string maker;
    public string region;
    public string animal_type;
    public string suit_style;
    public string suit_type;
    public List<string> suit_color;
}