using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WebApi;
using SepehrHotelApiSampleClient.Availability.SearchByCityAndDate.WinForms;
using SepehrHotelApiSampleClient.Booking.Book.WinForms;
using SepehrHotelApiSampleClient.Booking.Book.WinForms.BookCommand;
using SepehrHotelApiSampleClient.BuiltinTypeExtensions;
using RequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V2.GetByFromDateAndToDate.RequestModel;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V2.GetByFromDateAndToDate.ResponseModel;
using ViewModel = SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WinForms.ViewModels;
using LockRequestModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Booking.V2.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)
        {
            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();
        }

        private BindingSource _bnsHotels;
        private BindingSource _bnsRoomTypes;
        private BindingSource _bnsDateRanges;
        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();

            _bnsRoomTypes = new BindingSource();
            _bnsRoomTypes.DataSource = hotel.RoomTypeList;
            _bnsRoomTypes.PositionChanged += _bnsRoomTypes_PositionChanged;
            RoomTypesPositionChanged();

            gridRoomTypeList.DataSource = _bnsRoomTypes;
        }

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

        private void _bnsDateRanges_PositionChanged(object sender, EventArgs e)
        {
            
        }

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

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

            _bnsDateRanges = new BindingSource();
            _bnsDateRanges.DataSource = currentRoomType.DateRangeList;
            _bnsDateRanges.PositionChanged += _bnsDateRanges_PositionChanged;

            gridDateRangeList.DataSource = _bnsDateRanges;
        }

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

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