package epson.print.inkrpln;

import android.content.Context;
import android.content.SharedPreferences;


import epson.print.inkrpln.InkRplnInfoClient;

public class InkRplnRepository {
    private static final String KEY_APPEND_SERIAL = "append-serial";
    private static final String KEY_BUTTON_TYPE = "button_type";
    private static final String KEY_INK_REPLEN_INFO_SERVER_URL = "ink_replen_info_server_url";
    private static final String KEY_LANDING_PAGE_URL = "server-landing_page";
    private static final String KEY_SERVER_CHECK_DISABLE = "server_check_disable";
    private static final String KEY_SHOW_INVITATION = "invitation";
    private static final String PREFS_NAME_CONTROL_DATA = "inkDs-control";
    private static final String PREFS_NAME_PRINTER_INFO = "inkDs-server";
    private static final String PREFS_NAME_TMP_PRINTER_INFO = "tmp_inkDs-server";
    private boolean mTemporallySave = false;

    public InkRplnRepository(boolean z) {
        this.mTemporallySave = z;
    }

    public void clearServerData(@NonNull Context context) {
        getPrinterInfoPreferences(context).edit().clear().apply();
    }

    public void saveData(@NonNull Context context, InkRplnInfoClient.InkRsInfo inkRsInfo) {
        if (inkRsInfo != null) {
            SharedPreferences.Editor edit = getPrinterInfoPreferences(context).edit();
            if (inkRsInfo.landingUrl != null) {
                edit.putString(KEY_LANDING_PAGE_URL, inkRsInfo.landingUrl);
            } else {
                edit.remove(KEY_LANDING_PAGE_URL);
            }
            edit.putBoolean(KEY_SHOW_INVITATION, inkRsInfo.showInvitation);
            edit.putInt(KEY_BUTTON_TYPE, inkRsInfo.buttonType);
            edit.putBoolean(KEY_APPEND_SERIAL, inkRsInfo.appendSerial);
            edit.apply();
        }
    }

    @Nullable
    public InkRplnInfoClient.InkRsInfo getInfo(@NonNull Context context) {
        if (!InkReplenishSystem.isInkReplenishSystemEnabled(context)) {
            return null;
        }
        InkRplnInfoClient.InkRsInfo inkRsInfo = new InkRplnInfoClient.InkRsInfo();
        SharedPreferences printerInfoPreferences = getPrinterInfoPreferences(context);
        inkRsInfo.landingUrl = printerInfoPreferences.getString(KEY_LANDING_PAGE_URL, (String) null);
        inkRsInfo.showInvitation = printerInfoPreferences.getBoolean(KEY_SHOW_INVITATION, false);
        inkRsInfo.buttonType = printerInfoPreferences.getInt(KEY_BUTTON_TYPE, 0);
        inkRsInfo.appendSerial = printerInfoPreferences.getBoolean(KEY_APPEND_SERIAL, false);
        return inkRsInfo;
    }

    private SharedPreferences getPrinterInfoPreferences(@NonNull Context context) {
        return getPrinterInfoPreferences(context, this.mTemporallySave);
    }

    private SharedPreferences getPrinterInfoPreferences(@NonNull Context context, boolean z) {
        return context.getSharedPreferences(z ? PREFS_NAME_TMP_PRINTER_INFO : PREFS_NAME_PRINTER_INFO, 0);
    }

    public void saveInvitationDialogDisableValue(@NonNull Context context, boolean z) {
        getControlPreferences(context).edit().putBoolean(KEY_SERVER_CHECK_DISABLE, z).apply();
    }

    public boolean getInvitationDialogDisabledValue(@NonNull Context context) {
        return getControlPreferences(context).getBoolean(KEY_SERVER_CHECK_DISABLE, false);
    }

    public void saveInkReplenishServerUrl(@NonNull Context context, @Nullable String str) {
        getPrinterInfoPreferences(context).edit().putString(KEY_INK_REPLEN_INFO_SERVER_URL, str).apply();
    }

    @Nullable
    public String getInkReplenishServerUrl(@NonNull Context context) {
        return getPrinterInfoPreferences(context).getString(KEY_INK_REPLEN_INFO_SERVER_URL, (String) null);
    }

    public void saveBuyInkButtonOffReadyInkOff(@NonNull Context context) {
        InkRplnInfoClient.InkRsInfo inkRsInfo = new InkRplnInfoClient.InkRsInfo();
        inkRsInfo.buttonType = 3;
        saveData(context, inkRsInfo);
    }

    private SharedPreferences getControlPreferences(@NonNull Context context) {
        return context.getSharedPreferences(PREFS_NAME_CONTROL_DATA, 0);
    }

    public void savePermanently(@NonNull Context context) {
        SharedPreferences printerInfoPreferences = getPrinterInfoPreferences(context, true);
        SharedPreferences.Editor edit = getPrinterInfoPreferences(context, false).edit();
        if (printerInfoPreferences.contains(KEY_LANDING_PAGE_URL)) {
            edit.putString(KEY_LANDING_PAGE_URL, printerInfoPreferences.getString(KEY_LANDING_PAGE_URL, (String) null));
        } else {
            edit.remove(KEY_LANDING_PAGE_URL);
        }
        if (printerInfoPreferences.contains(KEY_BUTTON_TYPE)) {
            edit.putInt(KEY_BUTTON_TYPE, printerInfoPreferences.getInt(KEY_BUTTON_TYPE, 0));
        } else {
            edit.remove(KEY_BUTTON_TYPE);
        }
        if (printerInfoPreferences.contains(KEY_SHOW_INVITATION)) {
            edit.putBoolean(KEY_SHOW_INVITATION, printerInfoPreferences.getBoolean(KEY_SHOW_INVITATION, false));
        } else {
            edit.remove(KEY_SHOW_INVITATION);
        }
        if (printerInfoPreferences.contains(KEY_APPEND_SERIAL)) {
            edit.putBoolean(KEY_APPEND_SERIAL, printerInfoPreferences.getBoolean(KEY_APPEND_SERIAL, false));
        } else {
            edit.remove(KEY_APPEND_SERIAL);
        }
        if (printerInfoPreferences.contains(KEY_INK_REPLEN_INFO_SERVER_URL)) {
            edit.putString(KEY_INK_REPLEN_INFO_SERVER_URL, printerInfoPreferences.getString(KEY_INK_REPLEN_INFO_SERVER_URL, (String) null));
        } else {
            edit.remove(KEY_INK_REPLEN_INFO_SERVER_URL);
        }
        edit.apply();
        printerInfoPreferences.edit().clear().apply();
    }

    public void deleteTemporaryData(@NonNull Context context) {
        getPrinterInfoPreferences(context, true).edit().clear().apply();
    }
}
