using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace HSUCO_Cargo_Garage_Operation_Program.CustomForm
{
    public partial class LotsScreen : Form
    {
        private List<ResultSetUserListInformation> _resultSetUserLists;
        private int pageMax = 0;
        private int nowPage = 0;
        private int totalPage = 1;

        public LotsScreen(List<ResultSetUserListInformation> resultSetUserListInformation)
        {
            _resultSetUserLists = resultSetUserListInformation;
            InitializeComponent();
            this.StartPosition = FormStartPosition.Manual;

            var bounds = Screen.AllScreens[Screen.AllScreens.Length - 1].Bounds;

            this.ClientSize = new Size(bounds.Width, bounds.Height);
            this.Location = new Point(bounds.Left, bounds.Top);
            this.TopMost = true;

            int baseMargin = 5; // 내부 각테두리의 여백 사이즈
            int controlWidth = 340 + 5; // Lot 컨트롤러 가로사이즈 + 여백
            int controlHeight = 90 + 5; // Lot 컨트롤러 세로사이즈 + 여백
            int tempSize = 0; // 임시 연상용 사이즈
            int widthCount = 0; // 가로 최종 갯수
            while (true)
            {
                widthCount++;
                tempSize = baseMargin + (controlWidth * (widthCount));
                if (tempSize > this.flowLayoutPanel_Lots.Size.Width)
                {
                    widthCount--;
                    break;
                }
            }
            int heightCount = 0; // 세로 최종 갯수
            while (true)
            {
                heightCount++;
                tempSize = baseMargin + (controlHeight * (heightCount));
                if (tempSize > this.flowLayoutPanel_Lots.Size.Height)
                {
                    heightCount--;
                    break;
                }
            }
            pageMax = widthCount * heightCount; // 한페이지에 표시가능한 총 갯수
            if (pageMax < _resultSetUserLists.Count)
            {
                totalPage = _resultSetUserLists.Count / pageMax;
                if (_resultSetUserLists.Count % pageMax != 0)
                {
                    totalPage++;
                }

                Timer timer = new Timer();
                timer.Interval = 5000; //5초
                timer.Tick += Timer_Tick;
                timer.Start();
            }
            ViewChange();
        }

        private void Timer_Tick(object sender, EventArgs e)
        {
            ViewChange();
        }

        private void ViewChange()
        {
            nowPage++;
            if (nowPage > totalPage)
            {
                nowPage = 1;
            }
            label_Page.Text = $"{nowPage}/{totalPage}";
            if (totalPage == 1)
            {
                for (int i = 0; i < _resultSetUserLists.Count; i++)

                {
                    CustomUserControl.Lot lot = new CustomUserControl.Lot(_resultSetUserLists[i].Area, _resultSetUserLists[i].CargoVehicleNumber);
                    flowLayoutPanel_Lots.Controls.Add(lot);
                }
            }
            else
            {
                this.flowLayoutPanel_Lots.Controls.Clear();
                int totalCount = _resultSetUserLists.Count;
                int limit = nowPage * (pageMax);

                if (limit > _resultSetUserLists.Count)
                {
                    limit = _resultSetUserLists.Count;
                }
                int start = (nowPage - 1) * pageMax;

                for (int i = start; i < limit; i++)
                {
                    CustomUserControl.Lot lot = new CustomUserControl.Lot(_resultSetUserLists[i].Area, _resultSetUserLists[i].CargoVehicleNumber);

                    flowLayoutPanel_Lots.Controls.Add(lot);
                }
            }
        }

        private void button_Close_Click(object sender, EventArgs e)
        {
            Close();
        }
    }
}