using UnityEngine.UI;

namespace WS
{
    [ObjectSystem]
    public class LoadingControllerUpdateSystem : UpdateSystem<LoadingController>
    {
        public override void Update(LoadingController self)
        {
            self.Update();
        }
    }

    ///<summary>切换场景Load控制器</summary>
    public class LoadingController : Controller<LoadingView, LoadingModel>
    {
        ///<summary>切换场景组件</summary>
        private SceneChange ThisSceneChange;

        ///<summary>绑定View&Model</summary>
        protected override void BindingViewModel()
        {
            _Model.Bind(_View.Describe_Text, _Model.Describe);
            _Model.Bind(_View.Progress_Slider, _Model.Progress);
            _Model.Bind(_View.Progress_Text, _Model.ProgressText);
        }

        ///<summary>打开视图</summary>
        ///<param name="change">切换场景组件</param>
        ///<param name="describe">描述</param>
        public void OpenView(ViewShowModeType viewShowMode, SceneChange change, string describe)
        {
            OpenView(viewShowMode);
            ThisSceneChange = change;
            _Model.Describe.Value = describe;
        }

        public void Update()
        {
            if (ThisSceneChange != null)
            {
                _Model.Progress.Value = ThisSceneChange.Process;
                _Model.ProgressText.Value = (ThisSceneChange.Process * 100).ToString("f0") + "%";
            }
        }

        public override void Dispose()
        {
            base.Dispose();
            ThisSceneChange = null;
        }
    }

    ///<summary>切换场景Load视图</summary>
    public class LoadingView : View
    {
        ///<summary>描述文本</summary>
        public Text Describe_Text;
        ///<summary>进度文本</summary>
        public Text Progress_Text;
        ///<summary>进度条</summary>
        public Slider Progress_Slider;

        public override void LoadInit()
        {
            Group = ViewGroup.Loading;
            ReferenceCollector rc = UIGameObject.GetComponent<ReferenceCollector>();
            Describe_Text = rc.GetComponent<Text>("Describe_Text");
            Progress_Text = rc.GetComponent<Text>("Progress_Text");
            Progress_Slider = rc.GetComponent<Slider>("Progress_Slider");
        }
    }

    ///<summary>切换场景Load进度Model</summary>
    public class LoadingModel : Model
    {
        ///<summary>描述</summary>
        public BindableProperty<string> Describe;
        ///<summary>进度</summary>
        public BindableProperty<float> Progress;
        ///<summary>进度文本</summary>
        public BindableProperty<string> ProgressText;

        public override void InitProperty()
        {
            Describe = CreateProperty<string>();
            Progress = CreateProperty<float>();
            ProgressText = CreateProperty<string>();
        }
    }
}