using System;
using System.Globalization;
using FairyGUI;
using FGUI.Privacy_24;
using SGModule.MarkdownKit;
using UnityEngine;

namespace ChillConnect
{
    public class PrivacyUI : BaseUI
    {
        private PrivacyUICtrl ctrl;
        private PrivacyModel model;
        private com_privacy ui;

        private string content;

        private int isTerm;

        public PrivacyUI(PrivacyUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.PrivacyUI;
            this.ctrl = ctrl;
        }

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "Privacy_24";
            uiInfo.assetName = "com_privacy";
            uiInfo.layerType = UILayerType.Popup;
            uiInfo.isNeedOpenAnim = true;
            uiInfo.isNeedCloseAnim = true;
            uiInfo.isNeedUIMask = true;
        }

        #region 生命周期

        protected override void OnInit()
        {
        }

        protected override void OnClose()
        {
            ui?.FadeOut();
        }

        protected override void OnBind()
        {
            ui = baseUI as com_privacy;
        }

        protected override void OnOpenBefore(object args)
        {

            if (args != null)
            {
                isTerm = (int)args;

            }

            InitView();

            ui.show.selectedIndex = isTerm != 2 ? 0 : 1;
        }

        protected override void OnOpen(object args)
        {
            ui?.FadeIn();
        }

        #endregion

        private void InitView()
        {
            bool BisTerm = isTerm < 2 && isTerm == 1;
            string title = BisTerm ? "Terms of Service" : "Privacy Policy";
            ui.title.text = isTerm == 2 ? "View Credits" : title;

            // MarkdownKit.Instance.ShowAsRichText(ui.text_list, BisTerm ? "user" : "privacy",
            //     new Color(171f/255, 142f/255, 122f/255), (b, state) =>
            //     {

            //     });

            LoadKit.Instance.LoadAsset<TextAsset>("TextAsset", BisTerm ? "Term" : "Privacy", (txt) =>
            {
                content = txt.text;

                var str = getstr(content, 16327);

                foreach (var t in str)
                {
                    var aRichTextField = new GRichTextField();
                    aRichTextField.SetSize(GRoot.inst.width, 100);
                    aRichTextField.color = new Color(0.64f, 0.36f, 0.12f, 1f);
                    // aRichTextField.strokeColor = Color.black;
                    // aRichTextField.stroke = 1;
                    aRichTextField.textFormat.size = 44;
                    aRichTextField.autoSize = AutoSizeType.Height;
                    aRichTextField.AddRelation(GRoot.inst, RelationType.Width);
                    aRichTextField.text = t;
                    ui.text_list.AddChild(aRichTextField);
                }

                ui.text_list.EnsureBoundsCorrect();
            });

            ui.btn_close.SetClick(CtrlCloseUI);
        }

        public string[] getstr(string strs, int len)
        {
            double i = strs.Length;
            var array = new string[int.Parse(Math.Ceiling(i / len).ToString(CultureInfo.InvariantCulture))];
            for (var j = 0; j < array.Length; j++)
            {
                len = len <= strs.Length ? len : strs.Length;
                array[j] = strs[..len];
                strs = strs.Substring(len, strs.Length - len);
            }

            return array;
        }
    }
}
