using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Printing;
using System.Windows.Forms;
using Font = System.Drawing.Font;

namespace HSUCO_Cargo_Garage_Operation_Program
{
    internal class PrintFilingReceipt
    {
        private List<PrintFilingReceiptInformation> _printFilingReceiptInformation;
        private int _count;
        private PrintDocument printDocument;

        public PrintFilingReceipt(List<PrintFilingReceiptInformation> printFilingReceiptInformation)
        {
            _printFilingReceiptInformation = printFilingReceiptInformation;
            _count = 0;
            printDocument = new PrintDocument();
            printDocument.DocumentName = "접수증";
            printDocument.PrintPage += new PrintPageEventHandler(PrintPage);
            printDocument.DefaultPageSettings.Margins.Top = Convert.ToInt32(50);
            printDocument.DefaultPageSettings.Margins.Bottom = Convert.ToInt32(50);
            printDocument.DefaultPageSettings.Margins.Left = Convert.ToInt32(20);
            printDocument.DefaultPageSettings.Margins.Right = Convert.ToInt32(20);
        }

        public void Print()
        {
            PrintDialog printDialog = new PrintDialog();
            printDialog.Document = printDocument;
            if (printDialog.ShowDialog() == DialogResult.OK)
            {
                printDocument.Print();
            }
        }

        private void PrintPage(object sender, PrintPageEventArgs e)
        {
            int top = e.MarginBounds.Left + (int)e.PageSettings.PrintableArea.Y;
            int bottom = (int)e.PageSettings.PrintableArea.Height;
            int left = e.MarginBounds.Left + (int)e.PageSettings.PrintableArea.X;
            int right = (int)e.PageSettings.PrintableArea.Width;

            int totalWidth = (int)e.PageBounds.Width - (int)e.PageSettings.PrintableArea.X;
            int totalHeight = (int)e.PageBounds.Height - (int)e.PageSettings.PrintableArea.Y;
            e.Graphics.DrawRectangle(new Pen(Brushes.Black, 3), left, top,
               (right - (left * 2)), (bottom - (top * 2)));

            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left, top + 290), new Point((right - (left)), top + 290));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left, top + 340), new Point((right - (left)), top + 340));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left, top + 390), new Point((right - (left)), top + 390));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left, top + 440), new Point((right - (left)), top + 440));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left, top + 490), new Point((right - (left)), top + 490));

            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left + 280, top + 290), new Point(left + 280, top + 490));
            e.Graphics.DrawLine(new Pen(Brushes.Black, 1), new Point(left + 500, top + 440), new Point(left + 500, top + 490));

            CenterTextWriter($"접 수 증", new Font("맑은 고딕", 30, FontStyle.Bold), e.Graphics, totalWidth, top + 100);

            e.Graphics.DrawString($"제 {_printFilingReceiptInformation[_count].No}호", new Font("맑은 고딕", 12), Brushes.Black, left + 100, top + 250);
            e.Graphics.DrawString($"접수일 : {_printFilingReceiptInformation[_count].DateReception.ToString("yyyy-MM-dd")}", new Font("맑은 고딕", 12), Brushes.Black, left + 500, top + 250);

            e.Graphics.DrawString($"① 신청인", new Font("맑은 고딕", 12), Brushes.Black, left + 50, top + 300);
            e.Graphics.DrawString($"{_printFilingReceiptInformation[_count].Name}", new Font("맑은 고딕", 12), Brushes.Black, left + 300, top + 300);
            e.Graphics.DrawString($"② 생년월일(법인등록번호)", new Font("맑은 고딕", 12), Brushes.Black, left + 50, top + 350);
            e.Graphics.DrawString($"{_printFilingReceiptInformation[_count].PersonalNumber}", new Font("맑은 고딕", 12), Brushes.Black, left + 300, top + 350);
            e.Graphics.DrawString($"③ 주소", new Font("맑은 고딕", 12), Brushes.Black, left + 50, top + 400);
            string address = _printFilingReceiptInformation[_count].Address;
            if (_printFilingReceiptInformation[_count].Address.Length > 20)
            {
                int index = _printFilingReceiptInformation[_count].Address.IndexOf(' ', 20);
                if (index != -1)
                {
                    address = _printFilingReceiptInformation[_count].Address.Substring(0, index);
                    address += "\r\n";
                    address += _printFilingReceiptInformation[_count].Address.Substring(index, _printFilingReceiptInformation[_count].Address.Length - index);
                }
            }

            e.Graphics.DrawString($"{address}", new Font("맑은 고딕", 12), Brushes.Black, left + 300, top + 390);
            e.Graphics.DrawString($"④ 차량번호", new Font("맑은 고딕", 12), Brushes.Black, left + 50, top + 450);
            e.Graphics.DrawString($"화물차량", new Font("맑은 고딕", 12), Brushes.Black, left + 289, top + 440);
            e.Graphics.DrawString($"{_printFilingReceiptInformation[_count].CargoVehicleNumber}", new Font("맑은 고딕", 12), Brushes.Black, left + 310, top + 460);
            e.Graphics.DrawString($"일반차량", new Font("맑은 고딕", 12), Brushes.Black, left + 508, top + 440);
            e.Graphics.DrawString($"{_printFilingReceiptInformation[_count].PassengerVehicleNumber}", new Font("맑은 고딕", 12), Brushes.Black, left + 529, top + 460);

            CenterTextWriter("화성도시공사 사장", new Font("맑은 고딕", 20, FontStyle.Bold), e.Graphics, totalWidth, top + 800);
            _count++;
            e.HasMorePages = _printFilingReceiptInformation.Count > _count;
            if (!e.HasMorePages)
            {
                _count = 0;
            }
        }

        private void CenterTextWriter(string text, Font font, Graphics g, int totalWidth, int posY)
        {
            SizeF textSize = g.MeasureString(text, font);
            float textPosX = (totalWidth - textSize.Width) / 2;
            g.DrawString(text, font, Brushes.Black, textPosX, posY);
        }
    }
}