using NSUListView;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
using NSUListView;
public class mainImageItem : IUListItemView
{
    public string id = "";
    private string nowPath = "";
    public bool isFang = true;
    void Start()
    {   
    }

    // Update is called once per frame
    void Update()
    {
    }
    int onDownHandler(string tempid, string url, bool isSuccess, System.Object data)
    {
        if (isSuccess == true)
        {
            Image image = transform.Find("bg/tu").GetComponent<Image>();
            Texture2D texture2d = data as Texture2D;
            Sprite sprite = Sprite.Create(texture2d, new Rect(0, 0, texture2d.width, texture2d.height), new Vector2(0.5f, 0.5f));
            image.sprite = sprite;
            onLoadImgHandler(image);
        }
        else
        {
            utilsTools.addTimer_Sec(gameObject, "redownload", (string _) =>
            {
                utilsTools.httpDOWNIMG(this, url, this.nowPath,onDownHandler);
                return false;
            }, 3);
        }
        return -1;
    }
    public void onLoadImgHandler(Image img)
    {
        float width = img.sprite.texture.width;
        float height = img.sprite.texture.height;
        RectTransform rectTransform = img.GetComponent<RectTransform>();
        if (width == height)
        {
            rectTransform.localPosition = new Vector2(0, 0);
        }
        else
        {
            float bet = rectTransform.rect.width / rectTransform.rect.height;
            if(bet < 0.6)
            {
                float newHeight = rectTransform.rect.height * width / height;
                Vector2 newSize = new Vector2(rectTransform.rect.width, newHeight);
                rectTransform.sizeDelta = newSize;
                rectTransform.localPosition = new Vector2(0, 0 - (newHeight - newSize.x) / 2);
            }
        }
    }
    public void setData(netLableImageItem item)
    {
        this.id = item.getId();
        var data = utilsTools.getGameScenUserInfo(id);
        string path = "";// 
        if (data != null)
        {
            path = data.getLocalThumbnailPath();
        }
        else
        {
            utilsTools.setGameScenUserInfo(id, item.getLineUrl(), item.getColorUrl(), item.getJsonUrl(),item.file);
            data = utilsTools.getGameScenUserInfo(id);
            path = data.getLocalThumbnailPath();
        }
        Image image = transform.Find("bg/tu").GetComponent<Image>();
        if (this.nowPath == path) return;
        this.nowPath = path;
        if (utilsTools.IsExistIO(path) == true)
        {
            utilsTools.loadLocalImage(this, image, path,(bool isSuccess)=> {
                onLoadImgHandler(image);
            });
        }
        else
        {
            utilsTools.httpDOWNIMG(this, item.getSmallLineUrl(), path, (string id, string url, bool isSuccess, System.Object data) =>
            {
                return onDownHandler(id, url, isSuccess, data);
            });
        }
    }

    public override void SetData(object data)
    {
        setData(data as netLableImageItem);
    }
    public override Vector2 GetItemSize(object data)
    {
        return new Vector2(350, 350);
    }
    public void onClick()
    {
        utilsTools.sendEventObjectParams("clickImageViewHandler", id);
    }
}
