package epson.common;

import android.content.Context;

import com.epson.mobilephone.common.license.DefaultLicenseInfo;

import java.util.Locale;

public class IprintLicenseInfo extends DefaultLicenseInfo {
    private static final int LICENSE_VERSION_CODE = 70006;
    private static final int PRIVACY_STATEMENT_VERSION_CODE = 60600;

    public int getLicenseAgreement(@NonNull Context context) {
        IprintAppRecord iprintAppRecord = new IprintAppRecord();
        if (!iprintAppRecord.getLicenseAgreementValue(context)) {
            return -1;
        }
        int licenseAgreeVersionCode = iprintAppRecord.getLicenseAgreeVersionCode(context);
        return licenseAgreeVersionCode < LICENSE_VERSION_CODE ? licenseAgreeVersionCode < PRIVACY_STATEMENT_VERSION_CODE ? 0 : 1 : licenseAgreeVersionCode < PRIVACY_STATEMENT_VERSION_CODE ? 2 : 3;
    }

    public void setLicenceAgreement(@NonNull Context context) {
        new IprintAppRecord().setLicenseAgreement(context, Math.max(LICENSE_VERSION_CODE, PRIVACY_STATEMENT_VERSION_CODE), true);
    }

    public String getDocumentString(@NonNull Context context, int i) {
        switch (i) {
            case 1:
                return getLicenseText(context);
            case 2:
                String language = Locale.getDefault().getLanguage();
                if (language == null || !language.equalsIgnoreCase(Locale.JAPAN.getLanguage())) {
                    return Utils.getAssetsFileContents(context, "iPrint_PRIVACY_STATEMENT20180322.txt");
                }
                return Utils.getAssetsFileContents(context, "iPrint_privacy_statement_jp20180122.txt");
            case 3:
                return Utils.getAssetsFileContents(context, "oss_license.txt");
            default:
                return super.getDocumentString(context, i);
        }
    }

    private String getLicenseText(@NonNull Context context) {
        return Utils.getAssetsFileContents(context, "License.txt");
    }

    public String getApplicationName(@NonNull Context context) {
        return context.getString(R.string.app_name);
    }

    public static boolean isAgreedCurrentVersion(@NonNull Context context) {
        return new IprintLicenseInfo().getLicenseAgreement(context) == 3;
    }

    public static void beforeLicenseCheck(@NonNull Context context) {
        if (!new IprintAppRecord().getLicenseAgreementValue(context)) {
            ExternalFileUtils.getInstance(context).clearRemotePrintersInfo();
            ExternalFileUtils.getInstance(context).clearIpPrintersInfo();
        }
    }
}
