using System;
using System.Drawing;
using System.Windows.Forms;
using System.Threading.Tasks;

namespace BokBonCheck
{
    public partial class DownloadProgressForm : Form
    {
        private ProgressBar progressBar;
        private Label lblStatus;
        private Label lblProgress;
        private Button btnCancel;
        private bool isCancelled = false;

        public DownloadProgressForm()
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterScreen;
        }

        private void InitializeComponent()
        {
            this.Text = "Chrome 드라이버 다운로드";
            this.Size = new Size(400, 150);
            this.StartPosition = FormStartPosition.CenterParent;
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.ControlBox = false;
            this.TopMost = true;

            // 상태 라벨
            lblStatus = new Label
            {
                Text = "Chrome 드라이버를 확인하고 있습니다...",
                Location = new Point(20, 20),
                Size = new Size(360, 20),
                Font = new Font("맑은 고딕", 9),
                TextAlign = ContentAlignment.MiddleCenter
            };

            // 진행률 라벨
            lblProgress = new Label
            {
                Text = "0%",
                Location = new Point(20, 50),
                Size = new Size(360, 20),
                Font = new Font("맑은 고딕", 10, FontStyle.Bold),
                TextAlign = ContentAlignment.MiddleCenter,
                ForeColor = Color.Blue
            };

            // 프로그레스 바
            progressBar = new ProgressBar
            {
                Location = new Point(20, 80),
                Size = new Size(360, 25),
                Style = ProgressBarStyle.Continuous,
                Minimum = 0,
                Maximum = 100,
                Value = 0
            };

            // 취소 버튼
            btnCancel = new Button
            {
                Text = "취소",
                Location = new Point(150, 115),
                Size = new Size(100, 30),
                Font = new Font("맑은 고딕", 9),
                BackColor = Color.LightCoral
            };

            btnCancel.Click += BtnCancel_Click;

            // 컨트롤 추가
            this.Controls.AddRange(new Control[]
            {
                lblStatus, lblProgress, progressBar, btnCancel
            });
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            isCancelled = true;
            btnCancel.Enabled = false;
            lblStatus.Text = "취소 중...";
        }

        public bool IsCancelled => isCancelled;

        public void UpdateProgress(int percentage, string status = null)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<int, string>(UpdateProgress), percentage, status);
                return;
            }

            progressBar.Value = Math.Min(percentage, 100);
            lblProgress.Text = $"{percentage}%";

            if (!string.IsNullOrEmpty(status))
            {
                lblStatus.Text = status;
            }
        }

        public void SetCompleted(string message = "다운로드 완료!")
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(SetCompleted), message);
                return;
            }

            progressBar.Value = 100;
            lblProgress.Text = "100%";
            lblStatus.Text = message;
            btnCancel.Text = "확인";
            btnCancel.BackColor = Color.LightGreen;
            btnCancel.Enabled = true;
        }

        public void SetError(string errorMessage)
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new Action<string>(SetError), errorMessage);
                return;
            }

            lblStatus.Text = "오류 발생";
            lblProgress.Text = "실패";
            lblProgress.ForeColor = Color.Red;
            btnCancel.Text = "확인";
            btnCancel.BackColor = Color.LightCoral;
            btnCancel.Enabled = true;
        }
    }
} 