﻿using Infinity.Core.Data;
using Infinity.Core.Interface;
using Infinity.Core.Kernel;
using Infinity.Core.Variant;
using System;

namespace Infinity.Client
{
    partial class CKernel : IKernel
    {
        private EntityManager _EntityManager = null;
        private DataCallbackManager _DataCallbackManager = null;
        private ModuleManager _ModuleManager = null;

        public CKernel()
        {
            _EntityManager = new EntityManager();
            _DataCallbackManager = new DataCallbackManager(this);
            _ModuleManager = new ModuleManager(this);
        }

        public string ResourcesPath { get; set; }

        public PersistID CreateEntitiy(string define)
        {
            IEntity entity = _EntityManager.CreateEntity(define);
            if (entity == null)
            {
                return PersistID.Empty;
            }

            return entity.Self;
        }

        public PersistID CreateEntitiy(PersistID root, string define)
        {
            IEntity entity = _EntityManager.CreateEntity(root, define);
            if (entity == null)
            {
                return PersistID.Empty;
            }

            _DataCallbackManager.CallEntityHandler(entity.Self, entity.Type, EntityEvents.OnCreate, new VariantList());

            return entity.Self;
        }

        public bool DestroyEntity(PersistID pid)
        {
            return _EntityManager.DelEntity(pid);
        }

        public PersistID GetRoot(PersistID pid)
        {
            IEntity entity = _EntityManager.GetEntity(pid);
            if (entity == null)
            {
                return PersistID.Empty;
            }

            return entity.Root;
        }

        public string GetType(PersistID pid)
        {
            IEntity entity = _EntityManager.GetEntity(pid);
            if (entity == null)
            {
                return Constants.NULL_STRING;
            }

            return entity.Type;
        }

        public bool IsExist(PersistID pid)
        {
            return _EntityManager.FindEntity(pid);
        }

        public void Initialize()
        {
        }

        public string ParseJson(PersistID pid)
        {
            throw new NotImplementedException();
        }

        public PersistID ParseEntity(string json)
        {
            throw new NotImplementedException();
        }
    }
}
