using System.Collections.Generic;
using SepehrHotelApiSampleClient.BuiltinTypeExtensions;
using ResponseModel = Sepehr.ApiGateway.Partners.Contracts.Hotel.Availability.V4.GetByFromDateAndToDate.ResponseModel;

namespace SepehrHotelApiSampleClient.Availability.GetByFromDateAndToDate.WinForms.ViewModels
{
    public class Hotel
    {
        public int SepehrGlobalId { get; set; }

        public string Name { get; set; }

        public List<StayDate> StayDateList { get; set; }

        public Hotel(ResponseModel.Hotel hotel)
        {
            SepehrGlobalId = hotel.SepehrGlobalId;
            Name = hotel.Name;
            StayDateList = hotel
                .StayDateList
                .ConvertAll(dateRange => ConvertDateRange(dateRange));
        }

        private StayDate ConvertDateRange(ResponseModel.StayDate dateRange)
        {
            var result = new StayDate
            {
                Date = dateRange.Date,
                Hotel = this,
            };

            result.RoomTypeList = dateRange
                .RoomTypeList
                .ConvertAll(roomType => ConvertRoomType(roomType, result));

            result.AmenityList = dateRange
                .AmenityList
                .ConvertAll(amenity => new Amenity
                {
                    SepehrGlobalId = amenity.SepehrGlobalId,
                    Name = amenity.Name,
                    CountingMethod = amenity.CountingMethod,
                    RateDetail_GuestAgeRangeStart = amenity.RateDetail.GuestAgeRangeStart,
                    RateDetail_GuestAgeRangeEnd = amenity.RateDetail.GuestAgeRangeEnd,
                    RateDetail_BoardPrice_TaxIncluded = amenity.RateDetail.BoardPrice_TaxIncluded.FormattedNumber(),
                    RateDetail_NetPrice_TaxExcluded = amenity.RateDetail.NetPrice_TaxExcluded.FormattedNumber(),
                    RateDetail_NetPrice_TaxIncluded = amenity.RateDetail.NetPrice_TaxIncluded.FormattedNumber(),
                    RateDetail_NetPrice_TaxIncluded_AsDecimal = amenity.RateDetail.NetPrice_TaxIncluded,
                });

            return result;
        }

        private RoomType ConvertRoomType(
            ResponseModel.RoomType roomType,
            StayDate dateRange)
        {
            var result = new RoomType
            {
                SepehrGlobalId = roomType.SepehrGlobalId,
                Name = roomType.Name,
                AdultCapacity = roomType.AdultCapacity,
                ChildCapacity = roomType.ChildCapacity,
                ChildAge = roomType.ChildAge,
                AvailableRoomCount = roomType.AvailableRoomCount,
                HasCheckinLimit = roomType.HasCheckinLimit,
                HasCheckoutLimit = roomType.HasCheckoutLimit,
                MinimumStay = roomType.MinimumStay,
                MaximumStay = roomType.MaximumStay,
                BoardTypeList = roomType
                    .BoardTypeList
                    .ConvertAll(boardType => new BoardType
                    {
                        Code = boardType.Code,
                        ExtrabedCapacity = boardType.ExtrabedCapacity,
                        Room_BoardPrice_TaxIncluded = boardType.Room_BoardPrice_TaxIncluded.FormattedNumber(),
                        Room_NetPrice_TaxIncluded = boardType.Room_NetPrice_TaxIncluded.FormattedNumber(),
                        Room_NetPrice_TaxIncluded_AsDecimal = boardType.Room_NetPrice_TaxIncluded,
                        Extrabed_BoardPrice_TaxIncluded = boardType.Extrabed_BoardPrice_TaxIncluded.FormattedNumber(),
                        Extrabed_NetPrice_TaxIncluded = boardType.Extrabed_NetPrice_TaxIncluded.FormattedNumber(),
                    }),
                DateRange = dateRange,
            };

            return result;
        }
    }
}
