using CloudBuilder.Core.Service;
using Krypton.Toolkit;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace CloudBuilder.Gui
{
    public partial class GridViewPagination : UserControl
    {
        public event Action OnPaginationChanged;
        public event Action OnPrint;
        public int Limit { get; set; } = new AbstractSearchCriteria().limit;
        private int OrgLimit;
        public int CurrentPage { get; set; } = 0;
        public int Page { get; set; } = 0;
        public int Total { get; set; } = 0;

        public GridViewPagination()
        {
            InitializeComponent();
            firstPageBut.Enabled = false;
            movePreviousBut.Enabled = false;
            moveNextBut.Enabled = false;
            moveLastBut.Enabled = false;
            printBut.Enabled = false;
        }

        public void SetLimit(int limit)
        {
            if (OrgLimit == 0) OrgLimit = limit;

            limitTxt.Text = limit.ToString();
            Limit = limit;
        }

        public void SetTotal(int total)
        {
            this.Total = total;
            if (Limit > 0)
                this.Page = Convert.ToInt32(Math.Ceiling(total * 1.0 / Limit));
            else
                this.Page = 1;

            if (total > 0 && CurrentPage == 0) CurrentPage = 1;
            else if (total == 0) CurrentPage = 0;

            totalRecordLabel.Text = Total.ToString();
            startPageLabel.Text = string.Format("{0} / {1}", CurrentPage, Page);

            if (total < 0) return;
            else if (total == 0)
            {
                firstPageBut.Enabled = false;
                movePreviousBut.Enabled = false;
                moveNextBut.Enabled = false;
                moveLastBut.Enabled = false;
                printBut.Enabled = false;
            }
            else if (CurrentPage == 1)
            {
                firstPageBut.Enabled = false;
                movePreviousBut.Enabled = false;
                printBut.Enabled = true;
                if (Page > CurrentPage)
                {
                    moveNextBut.Enabled = true;
                    moveLastBut.Enabled = true;
                }
                else
                {
                    moveNextBut.Enabled = false;
                    moveLastBut.Enabled = false;
                }
            }
            else if (CurrentPage == Page)
            {
                moveNextBut.Enabled = false;
                moveLastBut.Enabled = false;
                printBut.Enabled = true;
                if (Page > 1)
                {
                    firstPageBut.Enabled = true;
                    movePreviousBut.Enabled = true;
                }
                else
                {
                    firstPageBut.Enabled = false;
                    movePreviousBut.Enabled = false;
                }
            }
            else
            {
                firstPageBut.Enabled = true;
                movePreviousBut.Enabled = true;
                moveNextBut.Enabled = true;
                moveLastBut.Enabled = true;
                printBut.Enabled = true;
            }

        }

        public void Reset()
        {
            this.Limit = OrgLimit > 0 ? OrgLimit : new AbstractSearchCriteria().limit;
            this.CurrentPage = 1;
            this.limitTxt.Text = this.Limit.ToString();
            SetTotal(0);
        }

        private void limitTxt_Validated(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(this.limitTxt.Text))
            {
                this.Limit = 0;
                this.CurrentPage = 1;

                LimitChanged(this.Limit);
                return;
            }

            if (!int.TryParse(limitTxt.Text, out int limit)) return;

            if (this.Limit == limit) return;

            this.Limit = limit;

            this.CurrentPage = 1;

            LimitChanged(this.Limit);
        }

        public void LimitChanged(int limit)
        {
            OnPaginationChanged?.Invoke();
        }

        public void FirstPageChanged()
        {
            this.CurrentPage = 1;
            OnPaginationChanged?.Invoke();
        }

        public void MovePreviousChanged()
        {
            if (this.CurrentPage - 1 < 0)
            {
                return;
            }
            this.CurrentPage -= 1;
            OnPaginationChanged?.Invoke();
        }

        public void MoveNextChanged()
        {
            if (this.CurrentPage + 1 > this.Page)
            {
                return;
            }
            this.CurrentPage += 1;
            OnPaginationChanged?.Invoke();
        }
        public void MoveLastChanged()
        {
            this.CurrentPage = this.Page;
            OnPaginationChanged?.Invoke();
        }

        public void Print()
        {
            OnPrint?.Invoke();
            // 可以使用limit做其他处理
        }
        private void firstPageBut_Click(object sender, EventArgs e)
        {
            FirstPageChanged();
        }

        private void movePreviousBut_Click(object sender, EventArgs e)
        {
            MovePreviousChanged();
        }

        private void moveNextBut_Click(object sender, EventArgs e)
        {
            MoveNextChanged();
        }

        private void moveLastBut_Click(object sender, EventArgs e)
        {
            MoveLastChanged();
        }

        private void printBut_Click(object sender, EventArgs e)
        {
            Print();
        }

        private void limitTxt_KeyPress(object sender, KeyPressEventArgs e)
        {
            // 允许数字、控制字符(退格、删除等)   》》》》负号(如果需要负数) && e.KeyChar != '-'
            if (!char.IsControl(e.KeyChar) && !char.IsDigit(e.KeyChar))
            {
                e.Handled = true;
            }

            // 只允许一个负号，且必须在开头
            //if (e.KeyChar == '-' && ((KryptonTextBox)sender).Text.IndexOf('-') >= 0)
            //{
            //    e.Handled = true;
            //}
        }
    }
}
