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

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

        public string Name { get; set; }

        public List<DateRange> DateRangeList { get; set; }

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

        private DateRange ConvertDateRange(ResponseModel.DateRange dateRange)
        {
            var result = new DateRange
            {
                CheckinDate = dateRange.CheckinDate,
                CheckoutDate = dateRange.CheckoutDate,
                Hotel = this,
            };

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

            result.AmenityList = dateRange
                .AmenityList
                .ConvertAll(amenity => new Amenity
                {
                    SepehrGlobalId = amenity.SepehrGlobalId,
                    Name = amenity.Name,
                    RateDetailList = amenity
                        .RateDetailList
                        .ConvertAll(rateDetail => new Amenity_RateDetail
                        {
                            Date = rateDetail.Date,
                            GuestAgeRangeStart = rateDetail.GuestAgeRangeStart,
                            GuestAgeRangeEnd = rateDetail.GuestAgeRangeEnd,
                            BoardPrice_TaxIncluded = rateDetail.BoardPrice_TaxIncluded.FormattedNumber(),
                            NetPrice_TaxExcluded = rateDetail.NetPrice_TaxExcluded.FormattedNumber(),
                            NetPrice_TaxIncluded = rateDetail.NetPrice_TaxIncluded.FormattedNumber(),
                            NetPrice_TaxIncluded_AsDecimal = rateDetail.NetPrice_TaxIncluded,
                        }),
                });

            return result;
        }

        private RoomType ConvertRoomType(
            ResponseModel.RoomType roomType,
            DateRange dateRange)
        {
            var result = new RoomType
            {
                SepehrGlobalId = roomType.SepehrGlobalId,
                Name = roomType.Name,
                AdultCapacity = roomType.AdultCapacity,
                ChildCapacity = roomType.ChildCapacity,
                ChildAge = roomType.ChildAge,
                AvailableRoomCount = roomType.AvailableRoomCount,
                BoardTypeList = roomType
                    .BoardTypeList
                    .ConvertAll(boardType => new BoardType
                    {
                        Code = boardType.Code,
                        ExtrabedCapacity = boardType.ExtrabedCapacity,
                        RateDetailList = boardType
                            .RateDetailList
                            .ConvertAll(rateDetail => new BoardType_RateDetail
                            {
                                Date = rateDetail.Date,
                                Room_BoardPrice_TaxIncluded = rateDetail.Room_BoardPrice_TaxIncluded.FormattedNumber(),
                                Room_NetPrice_TaxExcluded = rateDetail.Room_NetPrice_TaxExcluded.FormattedNumber(),
                                Room_NetPrice_TaxIncluded = rateDetail.Room_NetPrice_TaxIncluded.FormattedNumber(),
                                Room_NetPrice_TaxIncluded_AsDecimal = rateDetail.Room_NetPrice_TaxIncluded,
                                Extrabed_BoardPrice_TaxIncluded = rateDetail.Extrabed_BoardPrice_TaxIncluded.FormattedNumber(),
                                Extrabed_NetPrice_TaxExcluded = rateDetail.Extrabed_NetPrice_TaxExcluded.FormattedNumber(),
                                Extrabed_NetPrice_TaxIncluded = rateDetail.Extrabed_NetPrice_TaxIncluded.FormattedNumber(),
                                Extrabed_NetPrice_TaxIncluded_AsDecimal = rateDetail.Extrabed_NetPrice_TaxIncluded,
                            }),
                    }),
                DateRange = dateRange,
            };

            return result;
        }
    }
}
