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

import com.aip.gateway.api.aip.AuthDelegateImpl;
import com.aip.gateway.api.aip.model.AipConfig;
import com.aip.gateway.api.aip.model.AipLabel;
import com.aip.gateway.api.aip.utils.Utilities;
import com.microsoft.informationprotection.ApplicationInfo;
import com.microsoft.informationprotection.Label;
import com.microsoft.informationprotection.MipContext;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.List;

@Slf4j
@Getter
public class AipFileManagerBase {
    private int lastErrNo;
    private String lastErrMsg;
    protected final AipConfig aipConfig;
    protected final ApplicationInfo appInfo;
    protected AuthDelegateImpl _authDelegate;
    protected MipContext _mipContext;

    protected final FileManager _fileManager;
    protected final PolicyManager _policyManager;
    protected final ProtectionManager _protectionManager;

    public AipFileManagerBase(AipConfig aipConfig) {
        resetError();
        this.aipConfig = aipConfig;
        this.appInfo = new ApplicationInfo(aipConfig.getClientId(), aipConfig.getAppName(), aipConfig.getAppVersion());
        this._fileManager = new FileManager();
        this._policyManager = new PolicyManager();
        this._protectionManager = new ProtectionManager();
    }
    public void dispose() {
        this.dispose(true);
    }
    protected void dispose(boolean disposing) {
        synchronized(this) {
            if (this._fileManager != null) {
                this._fileManager.dispose();
            }
            if (this._policyManager != null) {
                this._policyManager.dispose();
            }
            if (this._protectionManager != null) {
                this._protectionManager.dispose();
            }
            this._authDelegate = null;
        }
    }
    protected void resetError() {
        lastErrNo = 0;
        lastErrMsg = "";
    }
    protected void setError(int errNo, String errMsg1, String errMsg2) {
        lastErrNo = errNo;
        if (errMsg2 == null || errMsg2.isEmpty()) {
            lastErrMsg = errMsg1;
        }
        else {
            lastErrMsg = errMsg1 + "\r\n" + errMsg2;
        }
        log.error("AipFileManager::setError ==> {}, {}", errNo, lastErrMsg);
    }

    protected boolean checkLoading() {
        if (_authDelegate == null) {
            setError(998, "AipFileManager::AuthDelegate Failed.", "AIP AuthDelegate Is not Initialized.");
        }
        if (_mipContext == null) {
            setError(999, "AipFileManager::MipContext Failed.", "AIP MipContext Is not Initialized.");
        }
        return ((_authDelegate != null) && (_mipContext != null));
    }
    protected List<AipLabel> aipLabel2AppLabel(List<Label> labels) {
        if (labels == null) return new ArrayList<>();

        try {
            List<AipLabel> result = new ArrayList<>();
            labels.forEach(label -> {
                AipLabel aipLabel = Utilities.labelToAip(label);
                if (aipLabel != null) {
                    if (!label.getChildren().isEmpty()) {
                        label.getChildren().forEach(child -> {
                            var aipChildLabel = Utilities.labelToAip(child);
                            if (aipChildLabel != null) {
                                aipLabel.getChildren().add(aipChildLabel);
                            }
                        });
                    }
                    result.add(aipLabel);
                }
            });
            return result;
        }
        catch (Exception ex) {
            setError(31, "AipFileManager::aipLabel2AppLabel Failed.", ex.getMessage());
            return new ArrayList<>();
        }
    }

}
