using FWindSoft.Handle;
using FWindSoft.MVVM;
using FWindSoft.Wpf;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Com.FirmLib.UI.BllCommon
{
    public class AssetSelector : BaseViewModel
    {
        public AssetSelector()
        {
            InitRecommendFilter();
            CurrentPageIndex = 1;
            PageCount = 1;
        }
        #region 前台交互属性
        private string m_FilterName;
        /// <summary>
        /// 过滤名称
        /// </summary>
        public string FilterName
        {
            get { return this.m_FilterName; }
            set
            {
                this.m_FilterName = value;
                RaisePropertyChanged(nameof(this.FilterName));
            }
        }


        private int m_CurrentPageIndex;
        /// <summary>
        /// 当前页面索引
        /// </summary>
        public int CurrentPageIndex
        {
            get { return this.m_CurrentPageIndex; }
            set
            {
                this.m_CurrentPageIndex = value;
                RaisePropertyChanged(nameof(this.CurrentPageIndex));
            }
        }


        private int m_PageCount;
        /// <summary>
        /// 总页数
        /// </summary>
        public int PageCount
        {
            get { return this.m_PageCount; }
            set
            {
                this.m_PageCount = value;
                RaisePropertyChanged(nameof(this.PageCount));
            }
        }
        #endregion
        public PagerFilterControl FilterControl { get; private set; }
        private void InitRecommendFilter()
        {
            FilterControl = new PagerFilterControl();
            FilterControl.InitFilterAction = InitFilter;
            FilterControl.InitPagerAction = InitPager;
            FilterControl.UpdateData = UpdateData;
            FilterControl.FilterSetting["LocalName"] = string.Empty;
        }
        private void InitFilter(PagerFilterControl control)
        {
            this.FilterName = control.FilterSetting["LocalName"].ToString();
        }
        private void InitPager(PagerFilterControl control)
        {
            try
            {
                BeginInEdit();
                this.CurrentPageIndex = control.CurrentPageIndex;
            }
            finally
            {
                EndInEdit();
            }
        }
        private void UpdateData(PagerFilterControl control)
        {
            Refresh?.Invoke(this);
        }
        #region 前台交互命令
        /// <summary>
        /// 搜索命令
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void SearchCommand(object parameter)
        {
            try
            {
                FilterControl.FilterSetting["LocalName"] = this.FilterName;
                FilterControl.FilterChanged();
            }
            catch (Exception ex)
            {

                MessageTipEx.Show(ex);
            }
        }
        public bool CanSearchCommand(object parameter)
        {
            return true;
        }
        /// <summary>
        /// 分页切换命令
        /// </summary>
        /// <param name="parameter"></param>
        [Command]
        public void PageIndexChangedCommand(object parameter)
        {
            try
            {
                if (IsInEditing)
                    return;
                //设置当前的索引
                Int32.TryParse(parameter?.ToString()??string.Empty, out int index);
                FilterControl.CurrentPageIndex =Math.Max(index, 1);
                FilterControl.PageInfoChanged();
            }
            catch (Exception ex)
            {
              MessageTipEx.Show(ex);
            }
        }
        public bool CanPageIndexChangedCommand(object parameter)
        {
            return true;
        }
        #endregion

        /// <summary>
        /// 刷新数据委托
        /// </summary>
        public Action<AssetSelector> Refresh;
    }
}
