using System;
using System.Drawing;
using System.Windows.Forms;
using Elfisa.UI.Helpers;
using Elfisa.UI.Theming;
using Электронная_Фармация.Classes;
using Электронная_Фармация.Properties;

namespace Электронная_Фармация.HelpForms
{
    public partial class HF_LocationId : Form
    {
        private string _consigneeName = string.Empty;
        private string _consigneeAddress = string.Empty;

        public HF_LocationId()
        {
            InitializeComponent();
            Shown += (_, __) => WindowChromeHelper.ApplyTitleBarTheme(this, ThemeManager.Colors);
        }

        /// <summary>
        /// Показывает диалог Location ID. Возвращает true, если значение сохранено.
        /// </summary>
        public static bool PromptAndSave(IWin32Window owner)
        {
            using (var form = new HF_LocationId())
            {
                UiThemeHelper.ApplyToControlTree(form);
                return form.ShowDialog(owner) == DialogResult.OK;
            }
        }

        /// <summary>
        /// Показывает Location ID с контекстом: выбранная аптека (имя/адрес).
        /// </summary>
        public static bool PromptAndSave(IWin32Window owner, string consigneeName, string consigneeAddress)
        {
            using (var form = new HF_LocationId())
            {
                form._consigneeName = consigneeName ?? string.Empty;
                form._consigneeAddress = consigneeAddress ?? string.Empty;
                UiThemeHelper.ApplyToControlTree(form);
                return form.ShowDialog(owner) == DialogResult.OK;
            }
        }

        private void HF_LocationId_Load(object sender, EventArgs e)
        {
            UiThemeHelper.ApplyToControlTree(this);
            WindowChromeHelper.ApplyDialogChrome(this, panelHeader, lblTitle);
            txtLocationId.Text = Settings.Default.LocationId ?? string.Empty;
            txtLocationId.SelectAll();
            txtLocationId.Focus();

            if (lblConsigneeName != null)
            {
                lblConsigneeName.Text = _consigneeName;
                lblConsigneeName.Visible = !string.IsNullOrWhiteSpace(_consigneeName);
            }

            if (lblConsigneeAddress != null)
            {
                lblConsigneeAddress.Text = _consigneeAddress;
                lblConsigneeAddress.Visible = !string.IsNullOrWhiteSpace(_consigneeAddress);
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            var locationId = (txtLocationId.Text ?? string.Empty).Trim();
            if (string.IsNullOrWhiteSpace(locationId))
            {
                UiDialogs.ShowError("Укажите Location ID торговой точки.", "Location ID", this);
                txtLocationId.Focus();
                return;
            }

            AppConfig.SetLocationId(locationId);
            AppDebugLog.Info("Auth", $"LocationId сохранён: {locationId}");
            DialogResult = DialogResult.OK;
            Close();
        }

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