using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Windows.Media;

using BeWo.ViewModel;
using BeWo.ViewModel.ListViewModel;

using BS.Shared.DataContracts;

namespace BeWo.View
{
    public class AssessmentSheetRow : INotifyPropertyChanged
    {
        private readonly AssessmentSheetCategoryDC _Category;

        private readonly long _CustomerOid;

        private readonly Dictionary<int, AssessmentSheetValueVM> _Day2Entries = new Dictionary<int, AssessmentSheetValueVM>();

        private readonly AssessmentSheetEntryListVM _EntryList;

        private DateTime _StartDate;

        public long? CategoryOid
        {
            get { return _Category.AssessmentSheetCategoryOid; }
        }


        public AssessmentSheetRow(AssessmentSheetEntryListVM entryList, AssessmentSheetCategoryDC category, long customerOid, DateTime startDate, bool showWeek)
        {
            _CustomerOid = customerOid;
            _StartDate = startDate;
            _EntryList = entryList;
            _Category = category;
            PossibleValues = category.PossibleValues.Select(dc => new AssessmentSheetValueVM(dc)).ToList();
            PossibleValues.Sort(AssessmentSheetValueVM.Compare);

            PossibleValues.Insert(0, new AssessmentSheetValueVM(new AssessmentSheetValueDC()) { Brush = Brushes.White, Description = "keine Angabe" });

            foreach (var entry in _EntryList.VMList.Where(vm => vm.CategoryOid.Equals(category.AssessmentSheetCategoryOid) && vm.Day.HasValue && vm.ValueOid.HasValue))
            {
                if (showWeek)
                {
                    TimeSpan ts = entry.Day.Value.Subtract(_StartDate);
                    _Day2Entries[(int) ts.TotalDays + 1] = PossibleValues.SingleOrDefault(vm => vm.DataContract.AssessmentSheetValueOid.Equals(entry.ValueOid));
                }
                else
                {
                    _Day2Entries[entry.Day.Value.Day] = PossibleValues.SingleOrDefault(vm => vm.DataContract.AssessmentSheetValueOid.Equals(entry.ValueOid));
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public string CategoryString
        {
            get
            {
                return this._Category.Description;
            }
        }

        public AssessmentSheetValueVM Day1
        {
            get
            {
                return this.Get(1);
            }

            set
            {
                this.Set(1, value);
            }
        }

        public AssessmentSheetValueVM Day10
        {
            get
            {
                return this.Get(10);
            }

            set
            {
                this.Set(10, value);
            }
        }

        public AssessmentSheetValueVM Day11
        {
            get
            {
                return this.Get(11);
            }

            set
            {
                this.Set(11, value);
            }
        }

        public AssessmentSheetValueVM Day12
        {
            get
            {
                return this.Get(12);
            }

            set
            {
                this.Set(12, value);
            }
        }

        public AssessmentSheetValueVM Day13
        {
            get
            {
                return this.Get(13);
            }

            set
            {
                this.Set(13, value);
            }
        }

        public AssessmentSheetValueVM Day14
        {
            get
            {
                return this.Get(14);
            }

            set
            {
                this.Set(14, value);
            }
        }

        public AssessmentSheetValueVM Day15
        {
            get
            {
                return this.Get(15);
            }

            set
            {
                this.Set(15, value);
            }
        }

        public AssessmentSheetValueVM Day16
        {
            get
            {
                return this.Get(16);
            }

            set
            {
                this.Set(16, value);
            }
        }

        public AssessmentSheetValueVM Day17
        {
            get
            {
                return this.Get(17);
            }

            set
            {
                this.Set(17, value);
            }
        }

        public AssessmentSheetValueVM Day18
        {
            get
            {
                return this.Get(18);
            }

            set
            {
                this.Set(18, value);
            }
        }

        public AssessmentSheetValueVM Day19
        {
            get
            {
                return this.Get(19);
            }

            set
            {
                this.Set(19, value);
            }
        }

        public AssessmentSheetValueVM Day2
        {
            get
            {
                return this.Get(2);
            }

            set
            {
                this.Set(2, value);
            }
        }

        public AssessmentSheetValueVM Day20
        {
            get
            {
                return this.Get(20);
            }

            set
            {
                this.Set(20, value);
            }
        }

        public AssessmentSheetValueVM Day21
        {
            get
            {
                return this.Get(21);
            }

            set
            {
                this.Set(21, value);
            }
        }

        public AssessmentSheetValueVM Day22
        {
            get
            {
                return this.Get(22);
            }

            set
            {
                this.Set(22, value);
            }
        }

        public AssessmentSheetValueVM Day23
        {
            get
            {
                return this.Get(23);
            }

            set
            {
                this.Set(23, value);
            }
        }

        public AssessmentSheetValueVM Day24
        {
            get
            {
                return this.Get(24);
            }

            set
            {
                this.Set(24, value);
            }
        }

        public AssessmentSheetValueVM Day25
        {
            get
            {
                return this.Get(25);
            }

            set
            {
                this.Set(25, value);
            }
        }

        public AssessmentSheetValueVM Day26
        {
            get
            {
                return this.Get(26);
            }

            set
            {
                this.Set(26, value);
            }
        }

        public AssessmentSheetValueVM Day27
        {
            get
            {
                return this.Get(27);
            }

            set
            {
                this.Set(27, value);
            }
        }

        public AssessmentSheetValueVM Day28
        {
            get
            {
                return this.Get(28);
            }

            set
            {
                this.Set(28, value);
            }
        }

        public AssessmentSheetValueVM Day29
        {
            get
            {
                return this.Get(29);
            }

            set
            {
                this.Set(29, value);
            }
        }

        public AssessmentSheetValueVM Day3
        {
            get
            {
                return this.Get(3);
            }

            set
            {
                this.Set(3, value);
            }
        }

        public AssessmentSheetValueVM Day30
        {
            get
            {
                return this.Get(30);
            }

            set
            {
                this.Set(30, value);
            }
        }

        public AssessmentSheetValueVM Day31
        {
            get
            {
                return this.Get(31);
            }

            set
            {
                this.Set(31, value);
            }
        }

        public AssessmentSheetValueVM Day4
        {
            get
            {
                return this.Get(4);
            }

            set
            {
                this.Set(4, value);
            }
        }

        public AssessmentSheetValueVM Day5
        {
            get
            {
                return this.Get(5);
            }

            set
            {
                this.Set(5, value);
            }
        }

        public AssessmentSheetValueVM Day6
        {
            get
            {
                return this.Get(6);
            }

            set
            {
                this.Set(6, value);
            }
        }

        public AssessmentSheetValueVM Day7
        {
            get
            {
                return this.Get(7);
            }

            set
            {
                this.Set(7, value);
            }
        }

        public AssessmentSheetValueVM Day8
        {
            get
            {
                return this.Get(8);
            }

            set
            {
                this.Set(8, value);
            }
        }

        public AssessmentSheetValueVM Day9
        {
            get
            {
                return this.Get(9);
            }

            set
            {
                this.Set(9, value);
            }
        }

        public List<AssessmentSheetValueVM> PossibleValues { get; set; }

        private AssessmentSheetValueVM Get(int day)
        {
            return this._Day2Entries.ContainsKey(day) ? this._Day2Entries[day] : null;
        }

        private void Set(int day, AssessmentSheetValueVM value)
        {
            DateTime date = _StartDate.AddDays(day - 1);
            if (value.IsNew)
            {
                value = null;
                this._Day2Entries.Remove(day);
            }
            else
            {
                this._Day2Entries[day] = value;
            }

            var list = this._EntryList.VMList.Where(vm => vm.CategoryOid.Equals(this._Category.AssessmentSheetCategoryOid) && vm.Day.HasValue && vm.Day.Value.Equals(date)).ToList();
            AssessmentSheetEntryVM entryVM = null;
            if (list.Count > 0)
                entryVM = list[0];
            if (list.Count > 1)
            {
                for (int i = 1; i < list.Count; i++)
                {
                    this._EntryList.VMList.Remove(list[i]);
                }

            }
            if (entryVM != null)
            {
                if (value != null)
                {
                    entryVM.ValueOid = value.DataContract.AssessmentSheetValueOid;
                }
                else
                {
                    this._EntryList.VMList.Remove(entryVM);
                }
            }
            else if (value != null)
            {
                this._EntryList.VMList.Add(new AssessmentSheetEntryVM(new AssessmentSheetEntryDC()) { CategoryOid = this._Category.AssessmentSheetCategoryOid, CustomerOid = this._CustomerOid, Day = date, ValueOid = value.DataContract.AssessmentSheetValueOid });
            }

            if (this.PropertyChanged != null)
            {
                this.PropertyChanged(this, new PropertyChangedEventArgs("Day" + day));
            }
        }
    }
}