package com.action.actionscaner;

import android.content.ContentResolver;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import sto.android.os.SystemProperties;
import android.text.TextUtils;
import android.util.Log;

public class ScanerApi {

    private static final String TAG = "ActScaner";
    private Context mContext;
    private ContentResolver resolver = null;
    private static final Uri ACTION_SCAN_URI = Uri.parse("content://com.action.actionscaner.scanerprovider");
    private static final Uri ACTION_SCAN_URI_ZEBRA = Uri.parse("content://com.action.zebrascaner.zebrascanerprovider");

    private static final String IS_ENABLE_SCANER = "is_enable_scaner";
    private static final String SET_ENABLE_SCANER = "set_enable_scaner";
    private static final String IS_VIBRATOR = "is_vibrator";
    private static final String SET_VIBRATOR = "set_vibrator";
    private static final String GET_PLAY_SOUND = "get_play_sound";
    private static final String IS_PLAY_SOUND = "is_play_sound";
    private static final String SET_PLAY_SOUND = "set_play_sound";
    private static final String GET_SCAN_DATA_OUTPUT = "get_scan_data_output";
    private static final String SET_SCAN_DATA_OUTPUT = "set_scan_data_output";
    private static final String IS_AUTO_INPUT = "is_auto_input";
    private static final String SET_AUTO_INPUT = "set_auto_input";
    private static final String IS_AUTO_JUMP = "is_auto_jump";
    private static final String SET_AUTO_JUMP = "set_auto_jump";
    private static final String IS_UNINTERRUPTED = "is_uninterrupted";
    private static final String SET_UNINTERRUPTED = "set_uninterrupted";
    private static final String SET_UNINTERRUPTED_TIME = "set_uninterrupted_time";
    private static final String GET_LIGHTS_MODE = "get_lights_mode";
    private static final String SET_LIGHTS_MODE = "set_lights_mode";
    private static final String GET_SCANER_PARAMETER = "get_scaner_parameter";
    private static final String SET_SCANER_PARAMETER = "set_scaner_parameter";
    private static final String GET_SCANER_ALL_PARAMETER = "get_scaner_all_parameter";
    private static final String SET_SCANER_ALL_PARAMETER = "set_scaner_all_parameter";
    private static final String IS_BOOL = "is_bool";
    private static final String CUSTOM_VALUE_KEY = "custom-value-key";
    private static final String GET_SCANER_INFO = "get_scaner_info";
    private static final String SCANER_ABOUT = "scaner_about";
    private static final String CUSTOM_SCAN = "custom_scan";
    private static final String CUSTOM_STOP_LONGLIGHT = "custom_stop_longlight";
    private String scannerType = null;

    public ScanerApi(Context context) {
        this.mContext = context;
        resolver = mContext.getContentResolver();
        scannerType = SystemProperties.get("persist.camera.scanner.vendor", "hsm");
        if(scannerType.equals("")||scannerType == null){
            scannerType = "hsm";
        }
        Log.e(TAG,"scannerType = "+scannerType);
    }

    public boolean isEnableScaner() {
        boolean isEnable = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, IS_ENABLE_SCANER, null, extras);
                    if (extras != null)
                        isEnable = extras.getBoolean(IS_ENABLE_SCANER, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, IS_ENABLE_SCANER, null, extras);
                    if (extras != null)
                        isEnable = extras.getBoolean(IS_ENABLE_SCANER, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }

        return isEnable;
    }

    public void setEnableScaner(boolean enabled) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras.putBoolean(SET_ENABLE_SCANER, enabled);
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_ENABLE_SCANER, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putBoolean(SET_ENABLE_SCANER, enabled);
                    extras = resolver.call(ACTION_SCAN_URI, SET_ENABLE_SCANER, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public boolean isVibrator() {
        boolean isvibrator = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();

        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, IS_VIBRATOR, null, extras);
                    if (extras != null)
                        isvibrator = extras.getBoolean(IS_VIBRATOR, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, IS_VIBRATOR, null, extras);
                    if (extras != null)
                        isvibrator = extras.getBoolean(IS_VIBRATOR, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
        return isvibrator;
    }

    public void setVibrator(boolean vibrator) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras.putBoolean(SET_VIBRATOR, vibrator);
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_VIBRATOR, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putBoolean(SET_VIBRATOR, vibrator);
                    extras = resolver.call(ACTION_SCAN_URI, SET_VIBRATOR, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public boolean isPlaySound() {
        boolean isSound = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, GET_PLAY_SOUND, null, extras);
                    if (extras != null) {
                        int soundsMode = 0;
                        if(extras.getInt(GET_PLAY_SOUND, 0)==0) {
                            isSound = false;
                        } else {
                            isSound = true;
                        }
                    }
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, IS_PLAY_SOUND, null, extras);
                    if (extras != null)
                        isSound = extras.getBoolean(IS_PLAY_SOUND, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
        return isSound;
    }

    public void setPlaySound(boolean isSound) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    if (isSound) {
                        extras.putInt(SET_PLAY_SOUND, 2);
                    } else {
                        extras.putInt(SET_PLAY_SOUND, 0);
                    }
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_PLAY_SOUND, null, extras);

                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putBoolean(SET_PLAY_SOUND, isSound);
                    extras = resolver.call(ACTION_SCAN_URI, SET_PLAY_SOUND, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public boolean isAutoInput() {
        boolean autoInput = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, GET_SCAN_DATA_OUTPUT, null, extras);
                    if (extras != null) {
                        int outPutType = 0;
                        if(extras.getInt(GET_SCAN_DATA_OUTPUT, 0)==0) {
                            autoInput = true;
                        } else {
                            autoInput = false;
                        }
                    }
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, IS_AUTO_INPUT, null, extras);
                    if (extras != null)
                        autoInput = extras.getBoolean(IS_AUTO_INPUT, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
        return autoInput;
    }

    public void setAutoInput(boolean autoInput) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    if (autoInput) {
                        extras.putInt(SET_SCAN_DATA_OUTPUT, 0);
                    } else {
                        extras.putInt(SET_SCAN_DATA_OUTPUT, 3);
                    }
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_SCAN_DATA_OUTPUT, null, extras);

                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putBoolean(SET_AUTO_INPUT, autoInput);
                    extras = resolver.call(ACTION_SCAN_URI, SET_AUTO_INPUT, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public boolean isAutoJump() {
        boolean autoJump = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras = resolver.call(ACTION_SCAN_URI, IS_AUTO_JUMP, null, extras);
            autoJump = extras.getBoolean(IS_AUTO_JUMP, false);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
        return autoJump;
    }

    public void setAutoJump(boolean autoJump) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras.putBoolean(SET_AUTO_JUMP, autoJump);
            extras = resolver.call(ACTION_SCAN_URI, SET_AUTO_JUMP, null, extras);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
    }

    public boolean isUninterrupted() {
        boolean is_uninterrupted = false;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, IS_UNINTERRUPTED, null, extras);
                    if (extras != null)
                        is_uninterrupted = extras.getBoolean(IS_UNINTERRUPTED, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, IS_UNINTERRUPTED, null, extras);
                    if (extras != null)
                        is_uninterrupted = extras.getBoolean(IS_UNINTERRUPTED, false);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
        return is_uninterrupted;
    }

    public void setUninterrupted(boolean uninterrupted) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras.putBoolean(SET_UNINTERRUPTED, uninterrupted);
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_UNINTERRUPTED, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putBoolean(SET_UNINTERRUPTED, uninterrupted);
                    extras = resolver.call(ACTION_SCAN_URI, SET_UNINTERRUPTED, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public void setUninterruptedTime(int uninterruptedTime) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (uninterruptedTime < 0) {
            return;
        }
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras.putInt(SET_UNINTERRUPTED_TIME, uninterruptedTime);
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SET_UNINTERRUPTED_TIME, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras.putInt(SET_UNINTERRUPTED_TIME, uninterruptedTime);
                    extras = resolver.call(ACTION_SCAN_URI, SET_UNINTERRUPTED_TIME, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public int getLightsMode() {
        int lightsMode = 3;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras = resolver.call(ACTION_SCAN_URI, GET_LIGHTS_MODE, null, extras);
            lightsMode = extras.getInt(GET_LIGHTS_MODE, 3);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
        return lightsMode;
    }

    public void setLightsMode(int lightsMode) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras.putInt(SET_LIGHTS_MODE, lightsMode);
            extras = resolver.call(ACTION_SCAN_URI, SET_LIGHTS_MODE, null, extras);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
    }

    public Bundle getScanerAllParameter() {
        Bundle extras = null;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            extras = new Bundle();
            extras = resolver.call(ACTION_SCAN_URI, GET_SCANER_ALL_PARAMETER, null, extras);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
        return extras;
    }

    public void setScanerAllParameter(Bundle bundle) {
        if (bundle == null) return;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = resolver.call(ACTION_SCAN_URI, SET_SCANER_ALL_PARAMETER, null, bundle);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
    }

    public Object getScanerParameter(String parameterKey) {
        Object object = null;
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras.putString(GET_SCANER_PARAMETER, parameterKey);
            extras = resolver.call(ACTION_SCAN_URI, GET_SCANER_PARAMETER, null, extras);
            object = extras.get(GET_SCANER_PARAMETER);
            //Log.d(TAG, "getScanerParameter value: " + object);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
        return object;
    }

    private void setScanerParameter(Bundle bundle) {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            //Bundle extras = new Bundle();
            //extras.putString(SET_SCANER_PARAMETER, parameter);
            bundle = resolver.call(ACTION_SCAN_URI, SET_SCANER_PARAMETER, null, bundle);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
    }

    public void setScanerParameter(String parameterKey, boolean parameter) {
        if (!TextUtils.isEmpty(parameterKey)) {
            Bundle extras = new Bundle();
            extras.putString(SET_SCANER_PARAMETER, parameterKey);
            extras.putBoolean(IS_BOOL, true);
            extras.putBoolean(CUSTOM_VALUE_KEY, parameter);
            setScanerParameter(extras);
        }
    }

    public void setScanerParameter(String parameterKey, String parameter) {
        if (!TextUtils.isEmpty(parameterKey)) {
            Bundle extras = new Bundle();
            extras.putString(SET_SCANER_PARAMETER, parameterKey);
            extras.putBoolean(IS_BOOL, false);
            extras.putString(CUSTOM_VALUE_KEY, parameter);
            setScanerParameter(extras);
        }
    }

	/*
	public void setScanerParameter(String parameterKey, int parameter) {
		if (!TextUtils.isEmpty(parameterKey)) {
	    	String para = parameterKey + "," + parameter;
	    	setScanerParameter(para);
		}
	}*/

    public String getScanerInfo() {
        String info = "";
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, SCANER_ABOUT, null, extras);
                    if (extras != null) {
                        info = extras.getString(SCANER_ABOUT);
                    }
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, GET_SCANER_INFO, null, extras);
                    if (extras != null)
                        info = extras.getString(GET_SCANER_INFO);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
        return info;
    }

    public void scan() {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        Bundle extras = new Bundle();
        switch(scannerType) {
            case "zebra":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI_ZEBRA) != null) {
                    extras = resolver.call(ACTION_SCAN_URI_ZEBRA, CUSTOM_SCAN, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI_ZEBRA);
                }
                break;

            case "hsm":
                if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
                    extras = resolver.call(ACTION_SCAN_URI, CUSTOM_SCAN, null, extras);
                } else {
                    Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
                }
                break;
        }
    }

    public void stopLongLight() {
        if (resolver == null)
            resolver = mContext.getContentResolver();
        if (resolver.acquireContentProviderClient(ACTION_SCAN_URI) != null) {
            Log.d(TAG, "find provider " + ACTION_SCAN_URI);
            Bundle extras = new Bundle();
            extras = resolver.call(ACTION_SCAN_URI, CUSTOM_STOP_LONGLIGHT, null, extras);
        } else {
            Log.d(TAG, "Could Not Find Provider " + ACTION_SCAN_URI);
        }
    }
}
