using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;

public class SmallLoginItem : MonoBehaviour
{
    bool isChooseBig;
    public RawImage iconImg;
    public TextMeshProUGUI textName;
    public AccountData ad;
    public void setData(AccountData ad,bool isChooseBig)
    {
        this.isChooseBig = isChooseBig;
        this.ad = ad;
        textName.text = ad.name;
        StartCoroutine(UnityWebRequestGetData(Application.streamingAssetsPath+ad.iconUrl));
    }
    Texture2D texture;

    IEnumerator UnityWebRequestGetData(string _url)
    {
        using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(_url))
        {
            yield return uwr.SendWebRequest();
            if (uwr.isHttpError || uwr.isNetworkError) Debug.Log(uwr.error);
            else
            {
                if (uwr.isDone)
                {
                    int width = 64;
                    int height = 64;
                    Texture2D texture2d = new Texture2D(width, height);
                    texture2d = DownloadHandlerTexture.GetContent(uwr);
                    iconImg.texture = texture2d;
                    Resources.UnloadUnusedAssets();
                }
            }
        }
    }

    private void OnDestroy()
    {
        Destroy(texture);
    }


    public void ChooseIcon()
    {
        UserManager.Instance.setNowUser(this.ad, this.transform, iconImg.texture);
        LoginDataManager.Instance.setOftenUseAcc(this.ad);
        LoginWindowsManager.Instance.gotologinPassWord();
    }
}
