using System;
using System.Diagnostics;
using System.Drawing;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.Availability.WinForms.ActiveRoutes;
using SepehrFlightApiSampleClient.Availability.WinForms.DateRange;
using SepehrFlightApiSampleClient.Availability.WinForms.SearchByRouteAndDate;
using SepehrFlightApiSampleClient.Booking.WinForms.Book;
using SepehrFlightApiSampleClient.Generic.WinForms;
using SepehrFlightApiSampleClient.Refund.WinForms;

namespace SepehrFlightApiSampleClient
{
    public partial class MainForm : Form
    {
        private readonly SearchByRouteAndDateForm _searchByRouteAndDateForm;
        private readonly PendingRequestForm _pendingRequestForm;
        private readonly BookedPnrForm _bookedPnrForm;

        public MainForm()
        {
            InitializeComponent();

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

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

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

            var retrieveBookingForm = new RetrieveBookingForm(this);
            retrieveBookingForm.TopLevel = false;
            retrieveBookingForm.Parent = tbgRefundRetrievePnr;
            retrieveBookingForm.Show();

            var getPenaltyForm = new GetPenaltyForm(this, retrieveBookingForm);
            getPenaltyForm.TopLevel = false;
            getPenaltyForm.Parent = tbgRefundGetPenalty;
            getPenaltyForm.Show();

            _pendingRequestForm = new PendingRequestForm(this);
            _pendingRequestForm.TopLevel = false;
            _pendingRequestForm.Parent = tbgRefundPendingRequests;
            _pendingRequestForm.Show();

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

            var currentBalanceForm = new CurrentBalanceForm(this);
            currentBalanceForm.TopLevel = false;
            currentBalanceForm.Parent = tbgCurrentBalance;
            currentBalanceForm.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)
        {
            _pendingRequestForm.DataBindGrid();
        }

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