using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


using ZendeskApi_v2;
using ZendeskApi_v2.HelpCenter;
using ZendeskApi_v2.Models.HelpCenter.Translations;

namespace CCNetLogReader
{
    public class zdConfig
    {
        public string locale = "en-us";
        public string subdomain = "dtsweb";
        public string username = "nathan.brown@dtsweb.com";
        public string apikey = "WNgdiDNgJGWkwUbDNukh1QrduiV0ugTsvMRKcuTY";
    }

    public class ZendeskClient
    {
        bool _connected = false;
        zdConfig _config = new zdConfig();
        ZendeskApi _zd;
        //ILogger _log;

        public ZendeskClient(zdConfig config = null)
        {
            //var logFactory = LoggerFactory.Create(b => b.AddSimpleConsole(
            //    s => {
            //        s.SingleLine = true;
            //        s.IncludeScopes = true;
            //        s.TimestampFormat = "yyyy-MM-dd HH:mm:ss ";
            //    }
            //));

            //_log = logFactory.CreateLogger<ZendeskClient>();

            //_log.LogInformation("Constructing ZendeskClient.");
            if (null != config)
                _config = config;
        }

        public void Connect()
        {
            //_log.LogInformation("Connecting to Zendesk.");
            _zd = new ZendeskApi(
                _config.subdomain, _config.username,
                _config.apikey, _config.locale);
            _connected = true;
        }

        public void UpdateArticle(long articleID, string Body)
        {
            //_log.LogInformation($"Updating Article: {articleID}.");
            if (!_connected) Connect();

            //var locales = _zd.Locales.GetAllLocales();
            //foreach (var l in locales.Locales)
            //{
            //    _log.LogInformation(l.Name);
            //}

            var article = _zd.HelpCenter.Articles.GetArticle(articleID);
            var translation = _zd.HelpCenter.Translations.ShowTranslationForArticle(articleID, _config.locale).Translation;

            //_log.LogInformation($"Updating Article: {article.Article.Title}.");
            translation.Body = Body;

            _zd.HelpCenter.Translations.UpdateArticleTranslation(translation);

            //_log.LogInformation("Updated Article.");
        }
    }
}

