﻿
using EntityExtensionForORM;
using OpenLearningPlayer.Shared.Auxiliary;
using SQLite.Net.Attributes;
using System;
using System.Linq;

namespace OpenLearningPlayer.Shared.Domain
{
    [Table(TableName)]
    public class LearningWordStrategy : Base
    {
        public const string TableName = "LearningWordsStrategies";

        public string Name { get { return Get(ref Name_); } set { Set(ref Name_, value); } }
        private string Name_;

        // SynchronizedObservableCollection<StrategyItem> StrategyItems  -> LearningWordStrategy
        [Ignore]
        [InverseProperty("LearningWordStrategy")]
        [CascadeDelete]
        public SynchronizedObservableCollection<StrategyItem> StrategyItems { get { return GetCollection(ref StrategyItems_); } set { SetCollection(ref StrategyItems_, value); } }
        private SynchronizedObservableCollection<StrategyItem> StrategyItems_;

        public LearningWordStrategy()  : base() {
            StrategyItems = new SynchronizedObservableCollection<StrategyItem>();
        }

        private StrategyItem GetStepByNumber(int number)
        {
            return StrategyItems.FirstOrDefault(x => x.number == number);
        }

        public static void InitializeWord(WordOfDictionary wordOfDictionary)
        {
            wordOfDictionary.CurrentStepOfLearning = 1;
            wordOfDictionary.DateOfRepetition = DateTime.Today.ConvertToUTC();
            wordOfDictionary.RepetitionPersent = 0;
        }

        public void SetNextStep(WordOfDictionary wrd)
        {
           
            // New word
            if (wrd.CurrentStepOfLearning == 0)
            {
                InitializeWord(wrd);
            }
            else
            {
                StrategyItem item = GetStepByNumber(wrd.CurrentStepOfLearning + 1);
                if (item == null) return;
                wrd.CurrentStepOfLearning = item.number;
                wrd.DateOfRepetition = DateTime.Today.AddDays(item.interval_from_last_step);
                wrd.RepetitionPersent = (byte)(wrd.CurrentStepOfLearning * 100 / StrategyItems.Count);
            }
        }

        public override string ToString() => Name;

        [Table(TableName)]
        public class StrategyItem : Base
        {
            public const string TableName = "LearningWordStrategies_StrategyItem";

            // LearningWordStrategy LearningWordStrategy 
            [NotNull]
            public UUID LearningWordStrategy_id { get { return GetEntityId(ref LearningWordStrategy_id_); } set { SetEntityId(ref LearningWordStrategy_id_, value); } }
            private UUID  LearningWordStrategy_id_;
            [Ignore]
            [ForeignKey("LearningWordStrategy_id")]
            public LearningWordStrategy LearningWordStrategy { get { return GetEntity(ref LearningWordStrategy_, ref LearningWordStrategy_id_); } set { SetEntity(ref LearningWordStrategy_, ref LearningWordStrategy_id_, value); } }
            private LearningWordStrategy LearningWordStrategy_;

            public int interval { get { return Get(ref interval_); } set { Set(ref interval_, value); } }
            private int interval_;

            public int interval_from_last_step { get { return Get(ref interval_from_last_step_); } set { Set(ref interval_from_last_step_, value); } }
            private int interval_from_last_step_;

            public int number { get { return Get(ref number_); } set { Set(ref number_, value); } }
            private int number_;

            public StrategyItem() : base() { }
        }

    }
}
