namespace ScrewsMaster
{
    using System;
    using FairyGUI;
    using UnityEngine;
    using System.Globalization;

    public class PrivacyUI : BaseUI
    {
        private PrivacyUICtrl ctrl;
        private PrivacyModel model;
        private FGUI.SC006_privacy.com_privacy ui;

        private string content;

        private bool isTerm;

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

        protected override void SetUIInfo(UIInfo uiInfo)
        {
            uiInfo.packageName = "SC006_privacy";
            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 FGUI.SC006_privacy.com_privacy;
        }

        protected override void OnOpenBefore(object args)
        {
            if (args != null)
            {
                isTerm = (bool)args;
            }

            InitView();
        }

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

        #endregion

        private void InitView()
        {
            ui.txt_title.text = isTerm ? "Terms of Service" : "Privacy Policy";

            LoadKit.Instance.LoadAsset<TextAsset>("TextAsset", isTerm ? "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.32f, 0.32f, 0.32f, 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.list_privacy.AddChild(aRichTextField);
                }

                ui.list_privacy.EnsureBoundsCorrect();
            });

            ui.closeButton.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;
        }
    }
}