package com.philisense.www.license.ftp;

import javax.security.auth.x500.*;

import com.philisense.www.license.*;
import com.philisense.www.utils.*;

import java.util.*;

public class LicenseManager extends com.philisense.www.license.LicenseManager
{
    private static final String FTP_KEYSTORE_PARAM;
    private static final String EQUAL_KEY_STORE_PARAMS;
    private static final String FTP_DAYS;
    private LicenseNotary ftpNotary;
    private static final String CN_FTP_USER;
    
    protected LicenseManager() {
    }
    
    public LicenseManager(final LicenseParam licenseParam0) {
        this.setLicenseParam0(licenseParam0);
    }
    
    @Override
    public synchronized void setLicenseParam(final com.philisense.www.license.LicenseParam licenseParam0) {
        this.setLicenseParam0(licenseParam0);
    }
    
    private void setLicenseParam0(final com.philisense.www.license.LicenseParam licenseParam) {
        final LicenseParam licenseParam2 = (LicenseParam)licenseParam;
        final KeyStoreParam ftpKeyStoreParam = licenseParam2.getFTPKeyStoreParam();
        if (ftpKeyStoreParam == null) {
            throw new NullPointerException(LicenseManager.FTP_KEYSTORE_PARAM);
        }
        if (ftpKeyStoreParam.equals(licenseParam2.getKeyStoreParam())) {
            throw new IllegalArgumentException(LicenseManager.EQUAL_KEY_STORE_PARAMS);
        }
        final int ftpDays = licenseParam2.getFTPDays();
        if (0 >= ftpDays || ftpDays > 365) {
            throw new IllegalArgumentException(new ObfuscatedString(new long[] { -8447909007698265338L, 3963998191526449092L }).toString());
        }
        super.setLicenseParam(licenseParam2);
    }
    
    @Override
    protected synchronized LicenseContent verify(final LicenseNotary licenseNotary) throws Exception {
        try {
            return super.verify(licenseNotary);
        }
        catch (Exception ex) {
            final LicenseParam licenseParam = (LicenseParam)this.getLicenseParam();
            final LicenseNotary ftpLicenseNotary = this.getFTPLicenseNotary();
            if (this.getLicenseKey() != null) {
                return super.verify(ftpLicenseNotary);
            }
            if (!licenseParam.isFTPEligible()) {
                throw new NoLicenseInstalledException(licenseParam.getSubject());
            }
            final LicenseContent ftpLicenseContent = licenseParam.createFTPLicenseContent();
            ftpLicenseContent.setNotAfter(this.ftpNotAfter(licenseParam.getFTPDays()));
            final LicenseContent install = this.install(this.create(ftpLicenseContent, ftpLicenseNotary), ftpLicenseNotary);
            licenseParam.removeFTPEligibility();
            licenseParam.ftpGranted(install);
            return install;
        }
    }
    
    @Override
    protected synchronized void initialize(final LicenseContent licenseContent) {
        if (licenseContent.getHolder() == null) {
            licenseContent.setHolder(new X500Principal(LicenseManager.CN_FTP_USER));
        }
        super.initialize(licenseContent);
    }
    
    protected synchronized LicenseNotary getFTPLicenseNotary() throws Exception {
        if (this.ftpNotary == null) {
            this.ftpNotary = new LicenseNotary(((LicenseParam)this.getLicenseParam()).getFTPKeyStoreParam());
        }
        return this.ftpNotary;
    }
    
    protected Date ftpNotAfter(final int n) {
        final Calendar instance = Calendar.getInstance();
        instance.setTime(com.philisense.www.license.LicenseManager.midnight());
        instance.add(5, n);
        return instance.getTime();
    }
    
    static {
        FTP_KEYSTORE_PARAM = new ObfuscatedString(new long[] { 6540942263743768505L, -6059958959638303164L, 6377465279464853391L }).toString();
        EQUAL_KEY_STORE_PARAMS = new ObfuscatedString(new long[] { 5508595700258006979L, 4658549383733098011L, 4696761698113697678L, 7544341226212718087L, 8821059208312938461L, 302546095417378367L, 4227466611521490562L, -1772332321125311578L }).toString();
        FTP_DAYS = new ObfuscatedString(new long[] { 8356037110043899424L, 4664078144517272142L }).toString();
        CN_FTP_USER = LicenseManager.CN + Resources.getString(new ObfuscatedString(new long[] { -6534999889298787160L, -4220048377725910008L }).toString());
    }
}
