using System;
using System.Globalization;
using System.Linq;
using System.Windows.Forms;
using SepehrFlightApiSampleClient.BuiltinTypeExtensions;
using SepehrFlightApiSampleClient.ChangedSchedulePassengers.WebApi;

namespace SepehrFlightApiSampleClient.ChangedSchedulePassengers.WinForms
{
    public partial class GetCharterForm : Form
    {
        private readonly MainForm _mainForm;

        public GetCharterForm(MainForm mainForm)
        {
            _mainForm = mainForm;
            InitializeComponent();
        }

        private void btnGetCharter_Click(object sender, EventArgs e)
        {
            var request = new Sepehr.ApiGateway.Partners.Contracts.Flight.ChangedSchedulePassengers.V1.Charter.Get.RequestModel.Request
            {
                Username = _mainForm.txtUsername.Text,
                Password = _mainForm.txtPassword.Text,
                RecordId = Convert.ToInt32(txtRecordId.Text),
            };

            var response = CharterGetApi.Post(_mainForm.txtUrl.Text, request);

            txtResponse.Text = response.SerializeToJsonWithFormatIndented();
            txtResponse.Visible = true;

            if (response
                .BookingList
                .Any())
            {
                txtRecordId.Text = response
                    .BookingList
                    .Max(x => x.RecordId)
                    .ToString(CultureInfo.InvariantCulture);
            }
            

            MessageBox.Show("Done :)");
        }
    }
}
