using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.InformationProtection;
using Microsoft.InformationProtection.Protection;

namespace AipGateway.AIP
{
    public class ProtectionManager : AbstractManager
    {
        private IProtectionProfile _profile = null;
        private IProtectionEngine _engine = null;
        public ProtectionManager()
        {
        }
        ~ProtectionManager() => this.Dispose(false);

        public override void Dispose()
        {
            this.Dispose(true);
            GC.SuppressFinalize((object)this);

        }
        protected virtual void Dispose(bool disposing)
        {
            lock (this)
            {
                if (_engine != null) _engine.Dispose();
                if (_profile != null) _profile.Dispose();
                _profile = null;
                _engine = null;
            }
        }

        public override bool CreateProfile(ref MipContext mipContext)
        {
            try
            {
                var profileSettings = new ProtectionProfileSettings(mipContext,
                    CacheStorageType.OnDisk,
                    new ConsentDelegateImplementation());

                // IProtectionProfile은 특정 애플리케이션에 대한 모든 SDK 작업의 루트입니다.
                _profile = MIP.LoadProtectionProfile(profileSettings);
            }
            catch (Exception e)
            {
                SetError(1, "ProtectionManager::CreateProfile Failed.", e.Message);
                return false;
            }
            return true;
        }

        public override bool CreateEngine(ref Identity identity, ref AuthDelegateImplementation authDelegate)
        {
            try
            {
                authDelegate.ResetError();
                var engineSettings = new ProtectionEngineSettings(identity.Email, authDelegate, "", "")
                {
                    Identity = identity
                };

                _engine = Task.Run(async () => await _profile.AddEngineAsync(engineSettings)).Result;

                Console.WriteLine("Protection Engine Templates ======================================================");
                var templates = _engine.GetTemplates();
                for (int ii = 0; ii < templates.Count; ii++)
                {
                    Console.WriteLine("{0}: {1}, {2}", ii.ToString(), templates[ii].Id + " : " + templates[ii].Name, templates[ii].Description);
                }
                Console.WriteLine("=======================================================================");
            }
            catch (Exception e)
            {
                if (authDelegate.LastErrNo != 0)
                {
                    SetError(authDelegate.LastErrNo, "ProtectionManager::CreateEngine Failed.", authDelegate.LastErrMsg);
                }
                else
                {
                    SetError(2, "ProtectionManager::CreateEngine Failed.", e.Message);
                }

                return false;
            }

            return true;
        }
        
        public List<TemplateDescriptor> GetTemplates()
        {
            return _engine.GetTemplates();
        }

    }
}
