using DBMigration.Models;

namespace DBMigration.Forms
{
    public partial class ConnectionForm : Form
    {
        public ConnectionInfo ConnectionInfo { get; private set; }

        public ConnectionForm()
        {
            InitializeComponent();
            this.Load += ConnectionForm_Load;
        }

        private void ConnectionForm_Load(object? sender, EventArgs e)
        {
            UpdateCredentialsFields();
            this.txtServer.Text = "10.131.15.18";
            this.txtDatabase.Text = "EE";
            this.txtUserId.Text = "eeuser";
            this.txtPassword.Text = "Amkor123!";
            this.chkWindowsAuth.Checked = false;
        }

        private void UpdateCredentialsFields()
        {
            bool isWindowsAuth = chkWindowsAuth.Checked;
            txtUserId.Enabled = !isWindowsAuth;
            txtPassword.Enabled = !isWindowsAuth;
        }

        private void InitializeComponent()
        {
            this.txtServer = new TextBox();
            this.txtDatabase = new TextBox();
            this.txtUserId = new TextBox();
            this.txtPassword = new TextBox();
            this.chkWindowsAuth = new CheckBox();
            this.btnConnect = new Button();
            this.btnCancel = new Button();
            this.SuspendLayout();

            // txtServer
            this.txtServer.Location = new Point(12, 12);
            this.txtServer.Size = new Size(200, 23);
            this.txtServer.PlaceholderText = "서버 이름";

            // txtDatabase
            this.txtDatabase.Location = new Point(12, 41);
            this.txtDatabase.Size = new Size(200, 23);
            this.txtDatabase.PlaceholderText = "데이터베이스 이름";

            // txtUserId
            this.txtUserId.Location = new Point(12, 70);
            this.txtUserId.Size = new Size(200, 23);
            this.txtUserId.PlaceholderText = "사용자 ID";

            // txtPassword
            this.txtPassword.Location = new Point(12, 99);
            this.txtPassword.Size = new Size(200, 23);
            this.txtPassword.PasswordChar = '*';
            this.txtPassword.PlaceholderText = "비밀번호";

            // chkWindowsAuth
            this.chkWindowsAuth.Location = new Point(12, 128);
            this.chkWindowsAuth.Size = new Size(200, 23);
            this.chkWindowsAuth.Text = "Windows 인증 사용";
            this.chkWindowsAuth.CheckedChanged += (s, e) => UpdateCredentialsFields();

            // btnConnect
            this.btnConnect.Location = new Point(12, 157);
            this.btnConnect.Size = new Size(95, 23);
            this.btnConnect.Text = "연결";
            this.btnConnect.Click += BtnConnect_Click;

            // btnCancel
            this.btnCancel.Location = new Point(117, 157);
            this.btnCancel.Size = new Size(95, 23);
            this.btnCancel.Text = "취소";
            this.btnCancel.Click += BtnCancel_Click;

            // ConnectionForm
            this.ClientSize = new Size(224, 192);
            this.Controls.AddRange(new Control[] {
                this.txtServer,
                this.txtDatabase,
                this.txtUserId,
                this.txtPassword,
                this.chkWindowsAuth,
                this.btnConnect,
                this.btnCancel
            });
            this.FormBorderStyle = FormBorderStyle.FixedDialog;
            this.MaximizeBox = false;
            this.MinimizeBox = false;
            this.StartPosition = FormStartPosition.CenterParent;
            this.Text = "데이터베이스 연결";
            this.ResumeLayout(false);
            this.PerformLayout();
        }

        private void BtnConnect_Click(object? sender, EventArgs e)
        {
            ConnectionInfo = new ConnectionInfo
            {
                ServerName = txtServer.Text,
                DatabaseName = txtDatabase.Text,
                UserId = txtUserId.Text,
                Password = txtPassword.Text,
                UseWindowsAuthentication = chkWindowsAuth.Checked
            };

            DialogResult = DialogResult.OK;
            Close();
        }

        private void BtnCancel_Click(object? sender, EventArgs e)
        {
            DialogResult = DialogResult.Cancel;
            Close();
        }

        private TextBox txtServer;
        private TextBox txtDatabase;
        private TextBox txtUserId;
        private TextBox txtPassword;
        private CheckBox chkWindowsAuth;
        private Button btnConnect;
        private Button btnCancel;
    }
}
