// 本脚本只在不存在时会生成一次。已存在不会再次生成覆盖

using System.IO;
using FairyGUI;
using Fantasy;
using UnityEngine;
using NBC;
using Newtonsoft.Json;
using UIPanel = NBC.UIPanel;

namespace NBF
{
    public partial class PreviewDetailsPanel : UIPanel
    {
        public ItemInfo ItemInfo;


        protected override void OnInit()
        {
            this.AutoAddClick(OnBtnClick);
        }

        protected override void OnShow()
        {
            ItemInfo = GetData() as ItemInfo;

            Quality.SetQuality(ItemInfo.Config.Quality);

            Basic.SetInfo(ItemInfo);

            // var model = PrefabsHelper.CreatePrefab(ItemInfo.Config.Model);
            Model.SetData(new ItemInfo()
            {
                Id = ItemInfo.Config.Id,
                ConfigId = ItemInfo.Config.Id,
                Count = 1,
            });
            // Model.SetBackground(Back);

            InputManager.OnUICanceled += OnUICanceled;
        }


        private void OnUICanceled(string action)
        {
            if (!IsTop) return;
            if (action == InputDef.UI.SubPrev)
            {
            }
            else if (action == InputDef.UI.SubNext)
            {
            }
            else if (action == InputDef.UI.Up)
            {
            }
            else if (action == InputDef.UI.Down)
            {
            }
        }

        private void OnBtnClick(GComponent btn)
        {
            if (btn == BtnSaveIcon)
            {
                Model.SaveRenderTextureToPNG(ItemInfo.Config.Id);
#if UNITY_EDITOR
                UnityEditor.AssetDatabase.Refresh();
#endif
            }
            else if (btn == BtnSaveSetting)
            {
                var setting = Model.ViewerSettings;
                var json = JsonUtility.ToJson(setting); //JsonConvert.SerializeObject(setting);
                //Assets/Resources/config/Viewer
                var savePath = Path.Combine(Application.dataPath, $"Resources/config/Viewer/{ItemInfo.ConfigId}.json");
                File.WriteAllText(savePath, json);
#if UNITY_EDITOR
                UnityEditor.AssetDatabase.Refresh();
#endif
            }
            else if (btn == BtnReSet)
            {
                // Model.ReSetSetting(ItemInfo.Config);
            }
        }

        
    }
}