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;
        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();

            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();

            _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();
        }

        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();
        }

        private void txtPassword_Leave(object sender, EventArgs e)
        {
            if (txtPassword.Text.Length != 32)
            {
                MessageBox.Show("Pasword is not MD5. Please enter MD5 of password");
            }
        }
    }
}
