﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TableTop.Domain.CMS.Interfaces;
using TableTop.Models.CMS.Models;
using TableTop.Repository.CMS.Interfaces;
using TableTop.Repository.CMS.Services;
using Umbraco.Core.Models;
using Umbraco.Web;

namespace TableTop.Domain.CMS.Services
{
    public class NuPickersService : INuPickersService
    {
        private IDittoService _dittoService;
        private UmbracoHelper _umbHelper;
        public NuPickersService(IDittoService ditto)
        {
            _umbHelper = new UmbracoHelper(UmbracoContext.Current);
            _dittoService = ditto;
        }
        public IEnumerable<KeyValuePair<string,string>> GetNPCDropdown(string campaignName, string ddType)
        {
            var root = _umbHelper.TypedContentAtRoot();
            var campaign = root.DescendantsOrSelf("campaign").Where(m => m.Name == campaignName).FirstOrDefault();
            if (campaign != null)
            {
                var npcs = campaign.DescendantsOrSelf(ddType);
                if (npcs != null)
                {
                    return npcs.Select(m => GetKeyValuePairFromItem(m));
                }
            }
            return null;
        }

        private KeyValuePair<string, string> GetKeyValuePairFromItem(IPublishedContent m)
        {
            var npc = _dittoService.ContentAs<NPCModel>(m);
            return new KeyValuePair<string, string>(m.Id.ToString(), npc.CharacterName);
        }
    }
}
