﻿using Arch.CFramework.AppInternals;
using Arch.CFramework.AppInternals.Comm;
using Arch.CFramework.AppInternals.Components;
using Arch.CFramework.AppInternals.Permission;
using Arch.CFramework.AppInternals.Serialization;
using Arch.CFramework.AppInternals.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace Arch.CFramework.CacheRefresh.Present
{

    internal class RefreshResult
    {
        public string FullName { get; set; }
        public bool IsSuccessful { get; set; }
        public string Message { get; set; }
    }


    internal class ResultCollection : ComponentBase
    {
        public List<RefreshResult> Results { get; set; }

    }


    internal class CacheRefreshItem
    {
        public string FullName { get; set; }
        public string Description { get; set; }

        public int Order { get; set; }

    }



    internal class CacheRefreshCollection : ComponentBase
    {
        private List<CacheRefreshItem> items = new List<CacheRefreshItem>();

        public List<CacheRefreshItem> RefreshItems
        {
            set
            {
                items = value;
            }
            get
            {
                return items;
            }
        }

    }

    internal class CacheRefreshPresent
    {
        private static object synObject = new object();
        static CacheRefreshPresent instance = null;

        public static CacheRefreshPresent Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (synObject)
                    {
                        if (instance == null)
                        {
                            instance = new CacheRefreshPresent();
                        }
                    }
                }
                return instance;
            }

        }

        internal string RefreshCache(string fullName, ISerializer serializer)
        {
            if (PermissionValidate.Instance.CheckChangePermission())
            {
                List<Arch.CFramework.RefreshResult> refreshResults = CacheRefreshManager.Instance.Refresh(fullName);
                ResultCollection resultCollection = new ResultCollection();
                resultCollection.Results = new List<RefreshResult>();

                foreach (var item in refreshResults)
                {
                    resultCollection.Results.Add(new RefreshResult() { IsSuccessful = item.IsSuccessful, Message = item.Message, FullName = CacheRefreshManager.Instance.GetFullName(item.Source) });
                }
                return serializer.Serializer(resultCollection, Encoding.UTF8);
            }
            else
            {
                var msg = new Result();
                msg.IsSuccess = false;
                msg.Message = "Sorry,执行刷新失败，您没有执行权限。";
                return msg.Read(serializer);
            }

        }

        internal string ListView(ISerializer serializer)
        {
            if (serializer is HTMLSerializer)
            {
                return serializer.Serializer(BuildCollections(), Encoding.UTF8);
            }
            else
            {
                return serializer.Serializer(BuildCollections(), Encoding.UTF8);
            }

        }

        private CacheRefreshCollection BuildCollections()
        {
            CacheRefreshCollection collection = new CacheRefreshCollection();
            string hostUrl = ContextHelper.GetHostUrl();
            string query = ContextHelper.FormatQueryString();

            collection.RefreshItems = CacheRefreshManager.Instance.FunRefreshs.OrderByDescending(item => item.Order).Select(item => new CacheRefreshItem() { FullName = CacheRefreshManager.Instance.GetFullName(item), Description = item.Description, Order = item.Order }).ToList();
            return collection;
        }

    }


}
