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 PrintCertificate
    {
        private readonly List<CertificateInformation> _certificateInformation;
        private readonly string _garageName;
        private int _count;
        private PrintDocument printDocument;
        public PrintCertificate(List<CertificateInformation> certificateDatas, string garageName)
        {
            _certificateInformation = certificateDatas;
            _garageName = garageName;
            _count = 0;
            printDocument = new PrintDocument();
            printDocument.DocumentName = "차고지 이용 증명서";
            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);

            printDocument.PrintPage += PrintPage;
            
        }
        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.DrawString($"발급번호 : {_certificateInformation[_count].IssueNumber}", new Font("맑은 고딕", 12), Brushes.Black,left + 10, top + 10);
            CenterTextWriter($"화물자동차 공영차고지 이용 증명서", new Font("맑은 고딕", 30, FontStyle.Bold), e.Graphics, totalWidth, top + 100);
            e.Graphics.DrawString($"■ 성　　명 : {_certificateInformation[_count].Name}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 250);
            e.Graphics.DrawString($"■ 생년월일 : {_certificateInformation[_count].PersonalNumber}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 300);
            e.Graphics.DrawString($"■ 주　　소 : {_certificateInformation[_count].Address}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 350);
            e.Graphics.DrawString($"■ 차량번호 : {_certificateInformation[_count].CargoVehicleNumber}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 400);
            e.Graphics.DrawString($"■ 주차구역 : {_certificateInformation[_count].Area}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 450);
            e.Graphics.DrawString($"■ 이용요금 : {string.Format("{0:N0} 원", _certificateInformation[_count].Amount)}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 500);
            e.Graphics.DrawString($"■ 이용기한 : {_certificateInformation[_count].StartDate.ToString("yyyy-MM-dd")} ~ {_certificateInformation[_count].EndDate.ToString("yyyy-MM-dd")}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 550);
            e.Graphics.DrawString($"■ 차 고 지 : {_garageName}", new Font("맑은 고딕", 12), Brushes.Black,left + 50, top + 600);

            CenterTextWriter($"위 차량에 대하여 {_garageName} 화물자동차 공영차고지 이용을 증명합니다.", new Font("맑은 고딕", 12, FontStyle.Bold), e.Graphics, totalWidth, top + 700);
            CenterTextWriter($"{_certificateInformation[_count].IssueDate.ToString("yyyy년    MM월    dd일")}", new Font("맑은 고딕", 15), e.Graphics, totalWidth, top + 800);
            CenterTextWriter("화성도시공사 사장", new Font("맑은 고딕", 20, FontStyle.Bold), e.Graphics, totalWidth, top + 900);
            _count++;
            e.HasMorePages = _certificateInformation.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);
        }

    }
}
