﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using SomeoneLike.Core.Extensions;
using SomeoneLike.Domain.Entities.Base;
using SomeoneLike.Core.Pages.Abstract;

namespace SomeoneLike.Domain.Entities
{
    public class Page : ContentPage
    {
        public Page() : base() {}

        public Page(int id, string url, string name, string typeName = null) : base(id, url, name, typeName ?? nameof(Page)) { }

        public override void SetValues(IDictionary<string, string> pageContentValues)
        {
            if (pageContentValues != null)
            {
                foreach (var value in pageContentValues)
                {
                    AddOrUpdateValue(value.Key, value.Value);
                }
            }
        }

        public override void AddOrUpdateValue(string key, string value)
        {
            var isNew = false;

            var pageValue = PageValues?.FirstOrDefault(i => i.Key.ToLower() == key.ToLower());

            if (pageValue == null)
            {
                isNew = true;

                pageValue = new PageContentValue()
                {
                    Key = key
                };
            }

            pageValue.Value = value;

            if (isNew)
            {
                PageValues?.Add(pageValue);
            }
        }

        public override ContentPageValue GetPageValue(string key)
        {
            key.NotNull(nameof(key));

            return PageValues?.FirstOrDefault(i => i.Key.ToLower() == key.ToLower());
        }
    }
}
