using System;
using System.Collections.Generic;
using System.Windows.Forms;
using SepehrHotelApiSampleClient.Availability.SearchByCityAndDate.WinForms;
using AvailabilityRequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V4.SearchByCityAndDateAndGuests.RequestModel;

namespace SepehrHotelApiSampleClient.Availability.SearchByCityAndDateAndGuests.WinForms
{
    public partial class RoomAndPassengerCountForm : Form
    {
        private List<AvailabilityRequestModel.Room> _requestRoomList;

        public RoomAndPassengerCountForm(List<AvailabilityRequestModel.Room> requestRoomList)
        {
            _requestRoomList = requestRoomList;
            InitializeComponent();



            var roomCount = requestRoomList.Count;
            cmbRoomCount.SelectedItem = roomCount.ToString();
        }


        private void cmbRoomCount_SelectedIndexChanged(object sender, EventArgs e)
        {
            var roomCount = Convert.ToInt16(cmbRoomCount.SelectedItem);



            roomsPanel.Controls.Clear();
            for (int roomIndex = roomCount - 1; roomIndex >= 0; roomIndex--)
            {
                var roomUserControl = new RoomUserControl(this);

                if (roomIndex < _requestRoomList.Count)
                {
                    var requestRoom = _requestRoomList[roomIndex];

                    roomUserControl.nudAdultCount.Value = requestRoom.AdultCount;

                    if (requestRoom.ChildAgeList.Count > 0)
                    {
                        roomUserControl.nudChildCount.Value = requestRoom.ChildAgeList.Count;

                        for (int childCounter = 0; childCounter < requestRoom.ChildAgeList.Count; childCounter++)
                        {
                            var requestChild = requestRoom.ChildAgeList[childCounter];

                            var cmbChild = (ComboBox)roomUserControl.Controls[$"cmbChild{childCounter + 1}Age"];
                            foreach (var item in cmbChild.Items)
                            {
                                var comboboxItem = (RoomUserControl.ComboboxItem)item;

                                if (comboboxItem.Value == requestChild)
                                {
                                    cmbChild.SelectedItem = comboboxItem;
                                }
                            }
                        }
                    }
                }
                

                roomUserControl.Dock = DockStyle.Top;
                roomsPanel.Controls.Add(roomUserControl);
            }
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            _requestRoomList = new List<AvailabilityRequestModel.Room>(); 
            var roomCount = Convert.ToByte(cmbRoomCount.SelectedItem);

            for (int counter = roomCount - 1; counter >= 0; counter--)
            {
                var roomUserControl = (RoomUserControl)roomsPanel.Controls[counter];

                var requestRoom = new AvailabilityRequestModel.Room
                {
                    AdultCount = Convert.ToByte(roomUserControl.nudAdultCount.Value),
                    ChildAgeList = new List<byte>(),
                };

                if (roomUserControl.nudChildCount.Value > 0)
                {
                    for (int childCounter = 1; childCounter <= roomUserControl.nudChildCount.Value; childCounter++)
                    {
                        var comboboxItem = (RoomUserControl.ComboboxItem)(((ComboBox)roomUserControl.Controls[$"cmbChild{childCounter}Age"]).SelectedItem);

                        if (
                            comboboxItem == null
                            ||
                            comboboxItem.Value == -1)
                        {
                            throw new Exception($"Please select child age");
                        }

                        requestRoom.ChildAgeList.Add(comboboxItem.ValueAsByte);
                    }
                }

                _requestRoomList.Add(requestRoom);
            }

            this.DialogResult = DialogResult.OK;
        }

        public List<AvailabilityRequestModel.Room> GetResult()
        {
            return _requestRoomList;
        }
    }
}
