using System;
using System.Windows.Forms;

namespace AyaNovaQBI
{
    public partial class InvoiceTemplateBuilder : Form
    {
        public InvoiceTemplateBuilder()
        {
            InitializeComponent();
        }

        TextBox _CurrentTextEditor = null;
        private void InvoiceTemplateBuilder_Load(object sender, EventArgs e)
        {
            btnCancel.Text = util.AyaTranslations["Cancel"];
            btnOK.Text = util.AyaTranslations["OK"];

            edHeader.Focus();
            edHeader.Text = util.QDat.InvoiceHeaderTemplate;
            edFooter.Text = util.QDat.InvoiceFooterTemplate;
            edUnit.Text = util.QDat.InvoiceUnitTemplate;

            edLoanItem.Text = util.QDat.InvoiceLoanItemTemplate;
            edMiscExpense.Text = util.QDat.InvoiceMiscExpenseTemplate;
            edOutsideService.Text = util.QDat.InvoiceOutsideServiceTemplate;
            edTravel.Text = util.QDat.InvoiceTravelTemplate;
            edService.Text = util.QDat.InvoiceServiceTemplate;

            _CurrentTextEditor = edHeader;
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            util.QDat.InvoiceFooterTemplate = edFooter.Text;
            util.QDat.InvoiceHeaderTemplate = edHeader.Text;
            util.QDat.InvoiceUnitTemplate = edUnit.Text;
            util.QDat.InvoiceLoanItemTemplate = edLoanItem.Text;
            util.QDat.InvoiceMiscExpenseTemplate = edMiscExpense.Text;
            util.QDat.InvoiceOutsideServiceTemplate = edOutsideService.Text;
            util.QDat.InvoiceTravelTemplate = edTravel.Text;
            util.QDat.InvoiceServiceTemplate = edService.Text;
            DialogResult = DialogResult.OK;
            Close();
        }

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

        private void editField_Enter(object sender, EventArgs e)
        {
            _CurrentTextEditor = (TextBox)sender;
            var edittag = _CurrentTextEditor.Tag.ToString();
            foreach (ToolStripMenuItem t in tbManager.Items)
            {
                var menutag = t.Tag.ToString();
                //System.Diagnostics.Debug.WriteLine($"editField_Enter examining menu item {menutag}");
                if (!menutag.StartsWith("Set"))//skip the two permanently visible ones
                {
                    t.Visible = edittag == menutag;
                    // System.Diagnostics.Debug.WriteLine($"editField_Enter setting menu item {menutag} to visible= {t.Visible}");
                }
            }
        }

        private void tbManager_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {
            if (e.ClickedItem.Tag == null) return;//probably just a container menu item / not a special menu item

            string tag = e.ClickedItem.Tag.ToString();
            // System.Diagnostics.Debug.WriteLine($"you clicked on {tag}");
            if (tag.StartsWith("~"))
                _CurrentTextEditor.Text = _CurrentTextEditor.Text.Insert(_CurrentTextEditor.SelectionStart, tag);
            else
            {
                switch (tag)
                {
                    case "SetDetailed":
                        edHeader.Text = "Charges for work order: ~WO#~";
                        edFooter.Text = "Service requested: ~ITEM_SUMMARY~";
                        edUnit.Text = "Unit Serviced: ~UNIT_NAME~";
                        edService.Text = "Service performed by: ~SERVICE_TECH~ Start Date & Time: ~SERVICE_START~  End Date & Time: ~SERVICE_STOP~\r\n" +
                        "Service Details: ~DETAILS~";
                        edTravel.Text = "Travel Start Date & Time: ~TRAVEL_START~";
                        edOutsideService.Text = "Outside Service Repair Charges: ~REPAIR_PRICE~  Outside Service Shipping Charges: ~SHIP_CHARGE~\r\n" +
                        "Date Sent: ~SENT~  Date Returned: ~RETURNED~";
                        edMiscExpense.Text = "Misc Expenses Summary: ~SUMMARY~";
                        edLoanItem.Text = "Item Loaned: ~ITEM~  Date Loaned: ~LOANED~  Date returned: ~LOAN_RETURNED~";
                        break;

                    case "SetBrief":
                        edHeader.Text = "Charges for work order: ~WO#~";
                        edFooter.Text = "";
                        edUnit.Text = "";
                        edService.Text = "";
                        edTravel.Text = "";
                        edOutsideService.Text = "";
                        edMiscExpense.Text = "";
                        edLoanItem.Text = "";
                        break;

                }

            }
        }
    }
}
