package com.epson.cameracopy.device;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Point;
import android.hardware.Camera;
import android.os.Environment;

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

public class CameraPreviewControl {
    @SuppressLint({"SdCardPath"})
    private static String DEFAULT_SAVE_DIRECTORY = "/EpsonCameraCapture";
    private static final String KEY_AUTO_PICTURE_MODE = "auto_picture_mode";
    private static final String KEY_GUIDE_MODE = "guide_mode";
    private static final String KEY_PICTURE_HEIGHT = "picture_size_height";
    private static final String KEY_PICTURE_RESOLUTION_MODE = "picture_resolution_mode";
    private static final String KEY_PICTURE_SIZE_LIST = "picture_size_list";
    private static final String KEY_PICTURE_WIDTH = "picture_size_width";
    private static final String KEY_SAVE_DIRECTORY = "save_directory";
    public static final int PICTURE_RESOLUTION_MODE_AUTO = 0;
    public static final int PICTURE_RESOLUTION_MODE_MANUAL = 1;
    private static final String PREFS_NAME = "camera_preview_option";
    private static CameraPreviewControl sCameraPreviewControl;
    private final Context mContext;

    public static CameraPreviewControl getInstance(Context context) {
        if (sCameraPreviewControl == null) {
            sCameraPreviewControl = new CameraPreviewControl(context);
        }
        return sCameraPreviewControl;
    }

    private CameraPreviewControl(Context context) {
        mContext = context.getApplicationContext();
    }

    public static boolean hasBackCamera() {
        int numberOfCameras = Camera.getNumberOfCameras();
        Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
        for (int i = 0; i < numberOfCameras; i++) {
            Camera.getCameraInfo(i, cameraInfo);
            if (cameraInfo.facing == 0) {
                return true;
            }
        }
        return false;
    }

    public boolean hasAutoPictureHardware(Context context) {
        if (hasBackCamera() && AptSensorAdapter.hasAptSensor(context)) {
            return true;
        }
        return false;
    }

    public boolean getGuideMode() {
        return mContext.getSharedPreferences(PREFS_NAME, 0).getBoolean(KEY_GUIDE_MODE, true);
    }

    public void setGuideMode(boolean z) {
        mContext.getSharedPreferences(PREFS_NAME, 0).edit().putBoolean(KEY_GUIDE_MODE, z).commit();
    }

    public boolean getAutoPictureMode() {
        return mContext.getSharedPreferences(PREFS_NAME, 0).getBoolean(KEY_AUTO_PICTURE_MODE, false);
    }

    public void setAutoPictureMode(Boolean bool) {
        mContext.getSharedPreferences(PREFS_NAME, 0).edit().putBoolean(KEY_AUTO_PICTURE_MODE, bool.booleanValue()).commit();
    }

    public String getSaveDirecotry() {
        return mContext.getSharedPreferences(PREFS_NAME, 0).getString(KEY_SAVE_DIRECTORY, Environment.getExternalStorageDirectory().getAbsolutePath() + DEFAULT_SAVE_DIRECTORY);
    }

    public void setSaveDirecotry(String str) {
        mContext.getSharedPreferences(PREFS_NAME, 0).edit().putString(KEY_SAVE_DIRECTORY, str).commit();
    }

    public void setPictureSize(Camera.Size size) {
        if (size != null) {
            setPictureSize(new Point(size.width, size.height));
        }
    }

    public void setPictureSize(Point point) {
        if (point != null) {
            SharedPreferences.Editor edit = mContext.getSharedPreferences(PREFS_NAME, 0).edit();
            edit.putInt(KEY_PICTURE_WIDTH, point.x);
            edit.putInt(KEY_PICTURE_HEIGHT, point.y);
            edit.commit();
            setPictureResolutionMode(1);
        }
    }

    public Point getPictureSize() {
        Point point = new Point();
        SharedPreferences sharedPreferences = mContext.getSharedPreferences(PREFS_NAME, 0);
        point.x = sharedPreferences.getInt(KEY_PICTURE_WIDTH, -1);
        point.y = sharedPreferences.getInt(KEY_PICTURE_HEIGHT, -1);
        return point;
    }

    public int getPictureResolutionMode() {
        return mContext.getSharedPreferences(PREFS_NAME, 0).getInt(KEY_PICTURE_RESOLUTION_MODE, 0);
    }

    private void setPictureResolutionMode(int i) {
        mContext.getSharedPreferences(PREFS_NAME, 0).edit().putInt(KEY_PICTURE_RESOLUTION_MODE, i).commit();
    }

    public void setPictureResolutionModeAuto() {
        setPictureResolutionMode(0);
    }

    public void setCameraPictureSizes(List<Camera.Size> list) {
        if (list != null) {
            StringBuilder sb = new StringBuilder();
            for (Camera.Size next : list) {
                if (sb.length() > 0) {
                    sb.append(":");
                }
                sb.append(Integer.toString(next.width));
                sb.append(PreferencesConstants.COOKIE_DELIMITER);
                sb.append(Integer.toString(next.height));
            }
            mContext.getSharedPreferences(PREFS_NAME, 0).edit().putString(KEY_PICTURE_SIZE_LIST, sb.toString()).commit();
        }
    }

    public List<Point> getCameraPictureSizes() {
        String[] split = mContext.getSharedPreferences(PREFS_NAME, 0).getString(KEY_PICTURE_SIZE_LIST, "").split(":");
        ArrayList arrayList = new ArrayList();
        for (String split2 : split) {
            String[] split3 = split2.split(PreferencesConstants.COOKIE_DELIMITER);
            if (split3.length >= 2) {
                int parseInt = Integer.parseInt(split3[0]);
                int parseInt2 = Integer.parseInt(split3[1]);
                if (parseInt > 0 && parseInt2 > 0) {
                    arrayList.add(new Point(parseInt, parseInt2));
                }
            }
        }
        return arrayList;
    }

    public static String getPictureSizeString(int i, int i2) {
        return String.format(Locale.US, "%4.1fM %dx%d", new Object[]{Double.valueOf(((i2 * i)) / 1000000.0d), Integer.valueOf(i), Integer.valueOf(i2)});
    }
}
