﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DNK.PriceListImport.MetroClubLoader
{
    public class Table
    {
        //--------------------------------------------------------------------------------------------
        private Dictionary<int, SurchargeChild> surchargeChildren;
        public Dictionary<int, SurchargeChild> SurchargeChildren { get { return surchargeChildren; } }
        public void AddSurchargeChild(int row, string value, string nights)
        {
            if (surchargeChildren == null)
                surchargeChildren = new Dictionary<int, SurchargeChild>();
            if (!surchargeChildren.ContainsKey(row))
            {
                surchargeChildren.Add(row, new SurchargeChild(value, nights));
            }
        }
        public int IndexLastSurchargeChild
        {
            get
            {
                return surchargeChildren == null ? -1 : surchargeChildren.Max(p => p.Key);
            }
        }
        public SurchargeChild GetSurchargeChild(int row)
        {
            if (surchargeChildren != null && surchargeChildren.ContainsKey(row))
                return surchargeChildren[row];
            return null;
        }
        //--------------------------------------------------------------------------------------------
        private Dictionary<int, SurchargePansion> surchargePansions;
        public Dictionary<int, SurchargePansion> SurchargePansions { get { return surchargePansions; } }
        public void AddSurchargePansion(int row, string value, string nights)
        {
            if (surchargePansions == null)
                surchargePansions = new Dictionary<int, SurchargePansion>();
            if (!surchargePansions.ContainsKey(row))
            {
                surchargePansions.Add(row, new SurchargePansion(value, nights));
            }
        }
        public int IndexLastSurchargePansion
        {
            get
            {
                return surchargePansions == null ? -1 : surchargePansions.Max(p => p.Key);
            }
        }
        public SurchargePansion GetSurchargePansion(int row)
        {
            if (surchargePansions != null && surchargePansions.ContainsKey(row))
                return surchargePansions[row];
            return null;
        }
        //--------------------------------------------------------------------------------------------
        private Hotel hotel;
        public Hotel Hotel { get { return hotel; } }
        //--------------------------------------------------------------------------------------------
        private StringBuilder _log = new StringBuilder();
        public string Log { get { return _log.ToString(); } }
        public bool ExistError()
        {
            if (hotel.HotelName == null || hotel.HotelName.Length == 0)
            {
                _log.AppendLine(string.Format("НЕ распознан отель: {0}", _source));
            }
            if (hotel.Pansion == null || hotel.Pansion.Length == 0)
            {
                _log.AppendLine(string.Format("НЕ распознано питание: {0}", _source));
            }
            if (hotel.IndexTypePrice < 0)
            {
                _log.AppendLine(string.Format("НЕ распознана цена за: {0}", _source));
            }
            if (hotel.IndexDurationPrice < 0)
            {
                _log.AppendLine(string.Format("НЕ распознана продолжительность цены: {0}", _source));
            }
            return _log.ToString().Length > 0;
        }
        //--------------------------------------------------------------------------------------------
        private string _source;
        public Table(string source)
        {
            _source = source;
            hotel = new Hotel(source);
        }
        //--------------------------------------------------------------------------------------------
        private Dictionary<int, RoomType> _roomTypes;
        public Dictionary<int, RoomType> RoomTypes { get { return _roomTypes; } }
        public void AddRoomType(int index, string roomType, string persons, string nights, string pansion, string week)
        {
            if (_roomTypes == null)
                _roomTypes = new Dictionary<int, RoomType>();

            string[] words = roomType.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);
            if (words.Length > 0 && !_roomTypes.ContainsKey(index))
                _roomTypes.Add(index, new RoomType(words[0], persons, nights, pansion, week));
        }
        public int IndexLastRoomType
        {
            get
            {
                return _roomTypes == null ? -1 : _roomTypes.Max(p => p.Key);
            }
        }
        public RoomType GetRoomType(int row)
        {
            if (_roomTypes != null && _roomTypes.ContainsKey(row))
                return _roomTypes[row];
            return null;
        }
        public void AddRoomType(int index, RoomType roomType)
        {
            if (_roomTypes == null)
                _roomTypes = new Dictionary<int, RoomType>();
            if (!_roomTypes.ContainsKey(index))
                _roomTypes.Add(index, roomType);
        }
        //--------------------------------------------------------------------------------------------
        private TitleTable titleTable;
        public TitleTable TitleTable { get { return titleTable; } }
        public void AddTitleTable(string name)
        {
            titleTable = new TitleTable();
        }
        //--------------------------------------------------------------------------------------------
        private int maxCountChildren = 0;
        public int MaxCountChildren { get { return maxCountChildren; } set { if (value > maxCountChildren) maxCountChildren = value; } } 
        //--------------------------------------------------------------------------------------------
    }
}
