using System;
using System.Collections.Generic;
using System.Globalization;
using System.Windows.Forms;
using SepehrHotelApiSampleClient.Booking.Book.WebApi;
using SepehrHotelApiSampleClient.Booking.Book.WinForms.InMemoryDatabase.BookedPnrs.Repository;
using SepehrHotelApiSampleClient.Booking.Book.WinForms.InMemoryDatabase.FailedRequests.Model;
using SepehrHotelApiSampleClient.Booking.Book.WinForms.InMemoryDatabase.FailedRequests.Repository;
using SepehrHotelApiSampleClient.Booking.Book.WinForms.PassengerUserControls;
using BookRequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Booking.V2.Book.RequestModel;
// ReSharper disable InconsistentNaming

namespace SepehrHotelApiSampleClient.Booking.Book.WinForms
{
    public partial class BookForm : Form
    {
        private readonly MainForm _mainForm;
        private readonly BookCommand.Command _command;

        public BookForm(
            MainForm mainForm,
            BookCommand.Command command)
        {
            _mainForm = mainForm;
            _command = command;
            
            InitializeComponent();

            SyncSelectedHotel();
        }

        private void SyncSelectedHotel()
        {
            txtBookYourLocalInventoryPnr.Text = Guid.NewGuid().ToString();
            txtTotalNetPrice.Text = _command.TotalNetPrice_TaxIncluded.ToString(CultureInfo.InvariantCulture);
            lblHotelName.Text = _command.HotelName;
            lblCheckin.Text = _command.CheckinDate.ToString("yyyy-MM-dd");
            lblCheckout.Text = _command.CheckoutDate.ToString("yyyy-MM-dd");


            roomsPanel.Controls.Clear();

            for (var roomIndex = _command.RoomList.Count - 1; roomIndex >= 0; roomIndex--)
            {
                var room = _command.RoomList[roomIndex];

                var roomUserControl = new RoomUserControl();
                roomUserControl.Dock = DockStyle.Top;
                roomUserControl.lblRoomNumber.Text = $"Room {roomIndex + 1} - {room.RoomTypeName}";
                roomsPanel.Controls.Add(roomUserControl);

                foreach (var childAge in room.ChildAgeList)
                {
                    var childUserControl = new ChildUserControl(childAge);
                    childUserControl.Dock = DockStyle.Top;
                    roomUserControl.passengersPanel.Controls.Add(childUserControl);
                }

                for (int counter = 0; counter < room.AdultCount; counter++)
                {
                    var adultUserControl = new AdultUserControl();
                    adultUserControl.Dock = DockStyle.Top;
                    roomUserControl.passengersPanel.Controls.Add(adultUserControl);
                }
            }
        }

        private void btnBook_Click(object sender, EventArgs e)
        {
            var request = new BookRequestModel.Request
            {
                Username = _mainForm.txtUsername.Text,
                Password = _mainForm.txtPassword.Text,
                LockId = _command.LockId,
                YourLocalInventoryPnr = txtBookYourLocalInventoryPnr.Text,
                MobileNumber = txtBookMobileNumber.Text,
                Email = txtBookEmail.Text,
                Remarks = string.Empty,
                RoomList = GetRequestRoomList(),
                TotalPayable = decimal.Parse(txtTotalNetPrice.Text),
            };

            try
            {
                var response = BookApi.Post(_mainForm.txtUrl.Text, request);

                var repositoryModel = ApiResponseModelToRepositoryModelConvertor.Convert(
                    response,
                    txtBookYourLocalInventoryPnr.Text);
                BookedPnrRepository.Add(repositoryModel);

                MessageBox.Show($"Done :) Your PNR is {response.Pnr}");
            }
            catch
            {
                FailedRequestRepository.Add(new FailedRequest
                {
                    Pnr = txtBookYourLocalInventoryPnr.Text,
                });
                throw;
            }
        }

        private List<BookRequestModel.Room> GetRequestRoomList()
        {
            var result = new List<BookRequestModel.Room>();

            for (var roomIndex = roomsPanel.Controls.Count - 1; roomIndex >= 0; roomIndex--)
            {
                var roomUserControl = (RoomUserControl)roomsPanel.Controls[roomIndex];

                var requestAdultList = new List<BookRequestModel.Guest>();
                foreach (Control passengerUserControl in roomUserControl.passengersPanel.Controls)
                {
                    var adultUserControl = passengerUserControl as AdultUserControl;
                    if (adultUserControl != null)
                    {
                        requestAdultList.Add(adultUserControl.GetGuestInfo());
                    }
                }

                var requestChildList = new List<BookRequestModel.Guest>();
                foreach (Control passengerUserControl in roomUserControl.passengersPanel.Controls)
                {
                    var childUserControl = passengerUserControl as ChildUserControl;
                    if (childUserControl != null)
                    {
                        requestChildList.Add(childUserControl.GetGuestInfo());
                    }
                }

                result.Add(new BookRequestModel.Room
                {
                    AdultGuestList = requestAdultList,
                    ChildGuestList = requestChildList,
                });
            }

            return result;
        }
    }
}
