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

using ModelData.BusinessModel.RDF;
using ModelData.BusinessModel.MainEntities;
using ModelData.BusinessModel.ExtraEntities;
using ModelData.BusinessModel.UnitOfWork;

namespace ModelData.BusinessModel
{
    public class BusinessDataManager
    {
        #region singl

        private static BusinessDataManager dataManager;
        public static BusinessDataManager Get()
        {
            if (dataManager == null)
                dataManager = new BusinessDataManager();

            return dataManager;
        }
        public static void Destroy()
        {
            dataManager = null;
        }

        private BusinessDataManager()
        {
            Init();
        }

        #endregion

        #region Data

        private bool needUpdate;
        private List<Settlement> infoAboutSettlements;
        private List<Region> allRegions;
        private List<SettlementType> allTypes;

        #endregion

        #region Private Methods

        private void Init()
        {
            needUpdate = true;
            infoAboutSettlements = new List<Settlement>();
            allRegions = new List<Region>();
            allTypes = new List<SettlementType>();
        }        

        #endregion

        #region Public Methods

        public bool Update
        {
            get { return needUpdate; }
            set { if (value) needUpdate = true; }
        }

        public Context GetSettlements(int YearMin, int YearMax)
        {
            Context context = new Context();
            context.GetAllSettlements(YearMin.ToString(), YearMax.ToString());
            return context;
        }

        public Settlement GetInfoAboutSettlement(string URL)
        {
            Settlement settlement;
            if (!needUpdate /*&& infoAboutSettlements.Exists(x => x.URL == URL)*/)
            {
                settlement = infoAboutSettlements.Find(x => x.URL == URL);
                if (settlement != null)
                    return settlement;
            }
            else
                infoAboutSettlements = new List<Settlement>();

            settlement = new Context().LoadInfoAboutSettlement(URL);
            infoAboutSettlements.Add(settlement);

            return settlement;
        }


        //  запрос всех имён за данный период
        public string[] GetSettlementsNames(string subName)
        {
            return new string[1];
        }

        public (int min, int max) GetYears()
        {
            var query_result = RDFQuery.GetData(RDFQuery.QueryType.Years, new string[] { });
            return (
                    min: int.Parse(query_result[0]["min"].ToString().Split('^')[0]),
                    max: int.Parse(query_result[0]["max"].ToString().Split('^')[0])
                    );
        }

        public Region[] AllRegions()
        {
            if (!needUpdate)
            {               
                if (allRegions.Count != 0)
                    return allRegions.ToArray();
            }
            else
                allRegions = new List<Region>();
            allRegions = new Context().LoadAllRegions();

            return allRegions.ToArray();
        }

        public SettlementType[] AllSettlementTypes()
        {
            if (!needUpdate)
            {
                if (allTypes.Count != 0)
                    return allTypes.ToArray();
            }
            else
                allTypes = new List<SettlementType>();
            allTypes = new Context().LoadAllTypes();

            return allTypes.ToArray();
        }

        #endregion

    }
}
