using System;
using FairyGUI;
using UnityEngine;
using BingoBrain.Core;
using BingoBrain.Asset;
using System.Globalization;

namespace BingoBrain
{
    public class JTermUI : BaseUI
    {
        private JTermUICtrl ctrl;
        private JTermModel model;
        private FGUI.JPrivacy.com_privacy ui;

        private string content;

        private bool isTerm;

        public JTermUI(JTermUICtrl ctrl) : base(ctrl)
        {
            uiName = UIConst.JTermUI;
            this.ctrl = ctrl;
        }

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



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

                var str = getstr(content, 16327);

                foreach (var t in str)
                {
                    var aRichTextField = new GTextField();
                    aRichTextField.SetSize(GRoot.inst.width, 100);
                    aRichTextField.color = new Color32(193,114,83,1);


                    aRichTextField.textFormat.size = 38;
                    aRichTextField.textFormat.bold=true;
                    aRichTextField.autoSize = AutoSizeType.Height;
                    aRichTextField.AddRelation(GRoot.inst, RelationType.Width);
                    aRichTextField.text = t;
                    ui.list_privacy.AddChild(aRichTextField);
                }


            });

            ui.closeButton.SetClick(CtrlCloseUI);
        }

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

            return myarray;
        }
    }
}