package com.aip.gateway.api.aip.manager;

import com.aip.gateway.api.aip.AuthDelegateImpl;
import com.aip.gateway.api.aip.ConsentDelegateImpl;
import com.microsoft.informationprotection.Identity;
import com.microsoft.informationprotection.MIP;
import com.microsoft.informationprotection.MipContext;
import com.microsoft.informationprotection.internal.protection.ProtectionEngineSettings;
import com.microsoft.informationprotection.internal.protection.ProtectionProfileSettings;
import com.microsoft.informationprotection.internal.protection.TemplateDescriptor;
import com.microsoft.informationprotection.protection.IProtectionEngine;
import com.microsoft.informationprotection.protection.IProtectionProfile;
import lombok.extern.slf4j.Slf4j;

import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Future;

@Slf4j
public class ProtectionManager extends AbstractManager {
    private IProtectionProfile _profile = null;
    private IProtectionEngine _engine = null;


    @Override
    public void dispose() {
        dispose(true);
    }
    void dispose(boolean disposing) {
        synchronized(this) {
            //if (_engine != null) _engine.Dispose();
            //if (_profile != null) _profile.Dispose();
            _profile = null;
            _engine = null;
        }
    }

    @Override
    public boolean createProfile(MipContext mipContext) {
        try {
            ConsentDelegateImpl consentDelegate = new ConsentDelegateImpl();

            ProtectionProfileSettings profileSettings = new ProtectionProfileSettings(mipContext,
                    com.microsoft.informationprotection.CacheStorageType.ON_DISK_ENCRYPTED,
                    consentDelegate);
            Future<IProtectionProfile> profileFuture = MIP.loadProtectionProfileAsync(profileSettings);
            _profile = profileFuture.get();
        }
        catch(InterruptedException ie) {
            setError(1, "ProtectionManager::createProfile InterruptedException Exception.", ie.getMessage());
            return false;
        }
        catch(ExecutionException eex) {
            setError(2, "ProtectionManager::createProfile ExecutionException Exception.", eex.getMessage());
            return false;
        }
        catch (Exception e) {
            setError(3, "ProtectionManager::createProfile Exception.", e.getMessage());
            return false;
        }
        return _profile != null;
    }

    @Override
    public boolean createEngine(Identity identity, AuthDelegateImpl authDelegate) {
        try {
            authDelegate.resetError();
            ProtectionEngineSettings engineSettings = new ProtectionEngineSettings(identity.getEmail(), authDelegate, "", "en-US");

            engineSettings.setIdentity(identity);

            // Add the engine and get the result.
            Future<IProtectionEngine> egineFuture = _profile.addEngineAsync(engineSettings);
            _engine = egineFuture.get();
        }
        catch(InterruptedException ie) {
            setError(1, "ProtectionManager::createEngine InterruptedException Exception.", ie.getMessage());
            return false;
        }
        catch(ExecutionException eex) {
            setError(2, "ProtectionManager::createEngine ExecutionException Exception.", eex.getMessage());
            return false;
        }
        catch (Exception e) {
            setError(3, "ProtectionManager::createEngine Exception.", e.getMessage());
            return false;
        }
        return _engine != null;
    }

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

}
