using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WebApi;
using SepehrHotelApiSampleClient.Availability.SearchByCityAndDate.WinForms;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V4.GetByFromDateAndToDate.RequestModel;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V4.GetByFromDateAndToDate.ResponseModel;
using ViewModel = SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WinForms.ViewModels;
using LockRequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Booking.V5.Lock.RequestModel;

namespace SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WinForms
{
    public partial class GetByFromDateAndToDateForm : Form
    {
        private readonly MainForm _mainForm;

        private List<ResponseModel.Hotel> _hotelList = new List<ResponseModel.Hotel>();

        public GetByFromDateAndToDateForm(MainForm mainForm)
        {
            _mainForm = mainForm;
            InitializeComponent();

            dtpFromDate.Value = DateTime.Now.AddDays(1);
            dtpToDate.Value = DateTime.Now.AddDays(4);
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                btnSearch.Enabled = false;

                var request = new RequestModel.Request
                {
                    Username = _mainForm.txtUsername.Text,
                    Password = _mainForm.txtPassword.Text,
                    FromDate = dtpFromDate.Value.ToString("yyyy-MM-dd"),
                    ToDate = dtpToDate.Value.ToString("yyyy-MM-dd"),
                };


                var response = GetByFromDateAndToDateApi.Post(_mainForm.txtUrl.Text, request);
                _hotelList = response.HotelList;

                DataBindHotelGrid();
            }
            finally
            {
                btnSearch.Enabled = true;
            }
        }

        private BindingSource _bnsHotels;
        private BindingSource _bnsRoomTypes;
        private BindingSource _bnsStayDates;
        private BindingSource _bnsAmenity;
        private void DataBindHotelGrid()
        {
            _bnsHotels = new BindingSource();

            var hotelsViewModel = _hotelList
                .Select(h => new ViewModel.Hotel(h))
                .ToList();

            _bnsHotels.DataSource = hotelsViewModel;

            gridHotelList.DataSource = _bnsHotels;
        }

        public ViewModel.Hotel GetCurrentHotelFromGrid()
        {
            return (ViewModel.Hotel)_bnsHotels?.Current;
        }

        private void gridHotelList_SelectionChanged(object sender, EventArgs e)
        {
            if (_bnsHotels.Current == null) return;

            var hotel = GetCurrentHotelFromGrid();

            _bnsStayDates = new BindingSource();
            _bnsStayDates.DataSource = hotel.StayDateList;
            _bnsStayDates.PositionChanged += _bnsStayDatesPositionChanged;
            StayDatesPositionChanged();

            gridStayDateList.DataSource = _bnsStayDates;
        }

        private void _bnsRoomTypes_PositionChanged(object sender, EventArgs e)
        {
            RoomTypesPositionChanged();
        }

        private void _bnsStayDatesPositionChanged(object sender, EventArgs e)
        {
            StayDatesPositionChanged();
        }

        private void StayDatesPositionChanged()
        {
            var currentStayDate = GetCurrentStayDateFromGrid();

            if (currentStayDate == null)
            {
                gridBoardTypeList.DataSource = null;
                return;
            }

            //bind room types
            _bnsRoomTypes = new BindingSource();
            _bnsRoomTypes.DataSource = currentStayDate.RoomTypeList;
            _bnsRoomTypes.PositionChanged += _bnsRoomTypes_PositionChanged;
            RoomTypesPositionChanged();

            gridRoomTypeList.DataSource = _bnsRoomTypes;

            //bind amenities
            _bnsAmenity = new BindingSource();
            _bnsAmenity.DataSource = currentStayDate.AmenityList;
            gridAmenityList.DataSource = _bnsAmenity;
        }

        private void RoomTypesPositionChanged()
        {
            var currentRoomType = GetCurrentRoomTypeFromGrid();

            if (currentRoomType == null)
            {
                gridStayDateList.DataSource = null;
                gridBoardTypeList.DataSource = null;
                return;
            }

            gridBoardTypeList.DataSource = currentRoomType.BoardTypeList;
        }

        public ViewModel.RoomType GetCurrentRoomTypeFromGrid()
        {
            if (_bnsRoomTypes?.Current == null) return null;

            return (ViewModel.RoomType)_bnsRoomTypes?.Current;
        }

        public ViewModel.StayDate GetCurrentStayDateFromGrid()
        {
            if (_bnsStayDates?.Current == null) return null;

            return (ViewModel.StayDate)_bnsStayDates?.Current;
        }
    }
}
