package com.epson.iprint.shared;

import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;

import java.util.Map;

import epson.common.Constants;
import epson.print.screen.PrintSetting;

public class ManagePreferences {
    public static final String PREFS_INFO_PRINT_CLONE = "PrintSettingClone";

    public boolean pushPreferrences(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("PrintSetting", 0);
        SharedPreferences.Editor edit = context.getSharedPreferences(PREFS_INFO_PRINT_CLONE, 0).edit();
        edit.clear();
        for (Map.Entry next : sharedPreferences.getAll().entrySet()) {
            Log.v("hoge PUSH", String.format("%s=%s", new Object[]{(String) next.getKey(), next.getValue()}));
            if (next.getValue() != null) {
                putInPrefs(edit, (String) next.getKey(), next.getValue());
            }
        }
        return edit.commit();
    }

    public boolean popPreferrences(Context context) {
        SharedPreferences sharedPreferences = context.getSharedPreferences("PrintSetting", 0);
        SharedPreferences sharedPreferences2 = context.getSharedPreferences(PREFS_INFO_PRINT_CLONE, 0);
        SharedPreferences.Editor edit = sharedPreferences.edit();
        for (Map.Entry next : sharedPreferences2.getAll().entrySet()) {
            Log.v("hoge POP", String.format("%s=%s", new Object[]{(String) next.getKey(), next.getValue()}));
            if (next.getValue() != null) {
                putInPrefs(edit, (String) next.getKey(), next.getValue());
            }
        }
        return edit.commit();
    }

    public void resetSavedFlag(Context context, String str) {
        context.getSharedPreferences(str, 0).edit().putInt(Constants.PREFS_INFO_PRINT_SAVE, 0).commit();
    }

    public int getSavedFlag(Context context, String str) {
        return context.getSharedPreferences(str, 0).getInt(Constants.PREFS_INFO_PRINT_SAVE, 0);
    }

    public boolean setPreferences(String str, Context context) {
        if (str.indexOf(EpsoniPrintSharedActivity.PACKAGE_PANASONIC_FAX) == -1) {
            return false;
        }
        PrintSetting printSetting = new PrintSetting(context, PrintSetting.Kind.photo);
        printSetting.loadSettings();
        printSetting.paperSizeValue = 0;
        printSetting.paperTypeValue = 0;
        printSetting.colorValue = 1;
        printSetting.layoutValue = 2;
        printSetting.copiesValue = 1;
        printSetting.saveSettings();
        return true;
    }

    public boolean setPreferences(int i, int i2, int i3, int i4, Context context) {
        PrintSetting printSetting = new PrintSetting(context, PrintSetting.Kind.photo);
        printSetting.loadSettings();
        printSetting.paperTypeValue = i;
        printSetting.paperSizeValue = i2;
        printSetting.colorValue = i3;
        printSetting.layoutValue = i4;
        printSetting.copiesValue = 1;
        printSetting.saveSettings();
        return true;
    }

    public static SharedPreferences.Editor putInPrefs(SharedPreferences.Editor editor, String str, Object obj) {
        Class<?> cls = obj.getClass();
        if (Boolean.class.isAssignableFrom(cls)) {
            editor.putBoolean(str, ((Boolean) obj).booleanValue());
        } else if (Integer.class.isAssignableFrom(cls)) {
            editor.putInt(str, ((Integer) obj).intValue());
        } else if (Float.class.isAssignableFrom(cls)) {
            editor.putFloat(str, ((Float) obj).floatValue());
        } else if (Long.class.isAssignableFrom(cls)) {
            editor.putLong(str, ((Long) obj).longValue());
        } else if (String.class.isAssignableFrom(cls)) {
            editor.putString(str, (String) obj);
        } else if (Bundle.class.isAssignableFrom(cls)) {
            Bundle bundle = (Bundle) obj;
            for (String str2 : bundle.keySet()) {
                putInPrefs(editor, getStoredBundleParamKey(str, str2), bundle.get(str2));
            }
        }
        return editor;
    }

    private static String getStoredBundleParamKey(String str, String str2) {
        return str + "__" + str2;
    }
}
