using System;
using System.Diagnostics;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Availability.WinForms.ActiveRoutes;
using SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate;
using SepehrFlightApiSampleClient.Booking.WinForms.Book;
using SepehrFlightApiSampleClient.BulkAvailability.WinForms.ClosedFlights;
using SepehrFlightApiSampleClient.BulkAvailability.WinForms.DateRange;
using SepehrFlightApiSampleClient.Generic.WinForms;
// ReSharper disable InconsistentNaming

namespace SepehrFlightApiSampleClient
{
    public partial class MainForm : Form
    {
        private readonly SearchByRouteAndDateForm _searchByRouteAndDateForm;
        private readonly Refund.WinForms.PendingRequestForm _refund_PendingRequestForm;
        private readonly BookedPnrForm _bookedPnrForm;
        private readonly FailedRequestsForm _failedRequestsForm;

        public MainForm()
        {
            InitializeComponent();

            var dateRangeForm = new DateRangeForm(this);
            dateRangeForm.TopLevel = false;
            dateRangeForm.Parent = tbgGetDateRange;
            dateRangeForm.Show();

            _searchByRouteAndDateForm = new SearchByRouteAndDateForm(
                this,
                dateRangeForm);
            _searchByRouteAndDateForm.TopLevel = false;
            _searchByRouteAndDateForm.Parent = tbgSearchByRouteAndDate;
            _searchByRouteAndDateForm.Show();

            var activeRoutesForm = new ActiveRoutesForm(this);
            activeRoutesForm.TopLevel = false;
            activeRoutesForm.Parent = tbgActiveRoutes;
            activeRoutesForm.Show();


            //refund
            var refund_RetrieveBookingForm = new Refund.WinForms.RetrieveBookingForm(this);
            refund_RetrieveBookingForm.TopLevel = false;
            refund_RetrieveBookingForm.Parent = tbgRefund_RetrievePnr;
            refund_RetrieveBookingForm.Show();

            var refund_GetPenaltyForm = new Refund.WinForms.GetPenaltyForm(this, refund_RetrieveBookingForm);
            refund_GetPenaltyForm.TopLevel = false;
            refund_GetPenaltyForm.Parent = tbgRefund_GetPenalty;
            refund_GetPenaltyForm.Show();

            _refund_PendingRequestForm = new Refund.WinForms.PendingRequestForm(this);
            _refund_PendingRequestForm.TopLevel = false;
            _refund_PendingRequestForm.Parent = tbgRefund_PendingRequests;
            _refund_PendingRequestForm.Show();

            //changePassengerInfo
            var changePassengerInfo_RetrieveBookingForm = new ChangePassengerInfo.WinForms.RetrieveBookingForm(this);
            changePassengerInfo_RetrieveBookingForm.TopLevel = false;
            changePassengerInfo_RetrieveBookingForm.Parent = tbgChangePassengerInfo_RetrievePnr;
            changePassengerInfo_RetrieveBookingForm.Show();

            var changePassengerInfo_GetPenaltyForm = new ChangePassengerInfo.WinForms.GetPenaltyForm(this, changePassengerInfo_RetrieveBookingForm);
            changePassengerInfo_GetPenaltyForm.TopLevel = false;
            changePassengerInfo_GetPenaltyForm.Parent = tbgChangePassengerInfo_GetPenalty;
            changePassengerInfo_GetPenaltyForm.Show();


            _bookedPnrForm = new BookedPnrForm(
                this, 
                refund_RetrieveBookingForm,
                changePassengerInfo_RetrieveBookingForm);
            _bookedPnrForm.TopLevel = false;
            _bookedPnrForm.Parent = tbgBookedPnrs;
            _bookedPnrForm.Show();

            _failedRequestsForm = new FailedRequestsForm(this);
            _failedRequestsForm.TopLevel = false;
            _failedRequestsForm.Parent = tbgFailedRequests;
            _failedRequestsForm.Show();
            

            var currentBalanceForm = new CurrentBalanceForm(this);
            currentBalanceForm.TopLevel = false;
            currentBalanceForm.Parent = tbgCurrentBalance;
            currentBalanceForm.Show();

            var accountingReportForm = new AccountingReportForm(this);
            accountingReportForm.TopLevel = false;
            accountingReportForm.Parent = tbgAccountingReport;
            accountingReportForm.Dock = DockStyle.Fill;
            accountingReportForm.Show();

            var changedSchedulePassengers = new ChangedSchedulePassengers.WinForms.GetCharterForm(this);
            changedSchedulePassengers.TopLevel = false;
            changedSchedulePassengers.Parent = tbgChangedSchedulePassengers;
            changedSchedulePassengers.Dock = DockStyle.Fill;
            changedSchedulePassengers.Show();


            var bulkClosedFlightsForm = new BulkClosedFlightsForm(this);
            bulkClosedFlightsForm.TopLevel = false;
            bulkClosedFlightsForm.Parent = tbgBulkClosedFlights;
            bulkClosedFlightsForm.Show();
        }

        private void MainForm_Load(object sender, EventArgs e)
        {
            ShowWarningForSmallScreenResolutions();

            //Put your username for easier debugging here
            if (Debugger.IsAttached)
            {
                txtUrl.Text = "https://192.168.1.123";
                txtUsername.Text = "TestDev1";
                txtPassword.Text = "25f9e794323b453885f5181f1b624d0b";
            }
        }

        private void ShowWarningForSmallScreenResolutions()
        {
            var myScreen = Screen.FromControl(this);
            var area = myScreen.WorkingArea;
            if (
                area.Width < 1152
                &&
                area.Height < 824
            )
            {
                MessageBox.Show(
                    "Your screen resolution is small and some controls may not show properly. " +
                    Environment.NewLine +
                    "Please use 1152x824 or larger size.");
            }
        }

        private void tbgSearchByRouteAndDate_Enter(object sender, EventArgs e)
        {
            _searchByRouteAndDateForm.SyncSelectedFlight();
        }

        private void tbgBookedPnrs_Enter(object sender, EventArgs e)
        {
            _bookedPnrForm.DataBindGrid();
        }

        private void tbgRefundPendingRequests_Enter(object sender, EventArgs e)
        {
            _refund_PendingRequestForm.DataBindGrid();
        }

        private void tbgHistory_Enter(object sender, EventArgs e)
        {
            _refund_PendingRequestForm.DataBindGrid();
            _bookedPnrForm.DataBindGrid();
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            if (txtUsername.Text.Equals("public", StringComparison.OrdinalIgnoreCase))
            {
                return;
            }

            if (txtPassword.Text.Length != 32)
            {
                MessageBox.Show("Pasword is not MD5. Please enter MD5 of password");
            }
        }
    }
}
