package epson.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import epson.print.ActivityIACommon;
import epson.print.R;

public class I2DoubleSideScanSettingActivity extends ActivityIACommon {
    private static final Boolean[] DOUBLE_SIDE_VALUE_LIST = {true, false};
    private static final String PARAM_KEY_DOUBLE_SIDE = "double-side";
    private static final String PARAM_KEY_TURN_DIRECTION = "turn-direction";
    private ModeListAdapter mDoubleSideAdapter;
    private ListView mDoubleSideListView;
    private ModeListAdapter mTurnDirectionAdapter;
    private ListView mTurnDirectionListView;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_i2_double_side_scan_setting);
        setTitle(R.string.str_settings_2sided_title);
        setActionBar(getTitle().toString(), true);
        mDoubleSideListView = (ListView) findViewById(R.id.upperListView);
        mDoubleSideAdapter = getDoubleSideAdapter();
        mDoubleSideListView.setAdapter(mDoubleSideAdapter);
        mDoubleSideListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                I2DoubleSideScanSettingActivity.this.changeDoubleSide(i);
            }
        });
        mTurnDirectionListView = (ListView) findViewById(R.id.lowerListView);
        mTurnDirectionAdapter = getTurnDirectionAdapter();
        mTurnDirectionListView.setAdapter(mTurnDirectionAdapter);
        mTurnDirectionListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                I2DoubleSideScanSettingActivity.this.changeTurnDirection(i);
            }
        });
        if (bundle != null) {
            updateUiFromBundle(bundle);
        } else {
            updateUiFromIntent(getIntent());
        }
    }

    private void updateUiFromBundle(Bundle bundle) {
        updateUi(bundle.getBoolean(PARAM_KEY_DOUBLE_SIDE), bundle.getInt(PARAM_KEY_TURN_DIRECTION));
    }

    private void updateUiFromIntent(Intent intent) {
        int i = 0;
        boolean z = intent != null && intent.getBooleanExtra(PARAM_KEY_DOUBLE_SIDE, false);
        if (intent != null) {
            i = intent.getIntExtra(PARAM_KEY_TURN_DIRECTION, 0);
        }
        updateUi(z, i);
    }

    private void updateUi(boolean z, int i) {
        mTurnDirectionAdapter.setPosition(i);
        mDoubleSideAdapter.setPosition(getDoubleSidePosition(z));
        updateTurnDirectionVisibilityFromDoubleSide();
    }

    private int getDoubleSidePosition(boolean z) {
        int i = 0;
        while (true) {
            Boolean[] boolArr = DOUBLE_SIDE_VALUE_LIST;
            if (i >= boolArr.length) {
                return 0;
            }
            if (boolArr[i].booleanValue() == z) {
                return i;
            }
            i++;
        }
    }

    private void changeTurnDirection(int i) {
        mTurnDirectionAdapter.setPosition(i);
    }

    private void changeDoubleSide(int i) {
        mDoubleSideAdapter.setPosition(i);
        updateTurnDirectionVisibilityFromDoubleSide();
    }

    private void updateTurnDirectionVisibilityFromDoubleSide() {
        mTurnDirectionListView.setVisibility(DOUBLE_SIDE_VALUE_LIST[mDoubleSideAdapter.getPosition()].booleanValue() ? 0 : 4);
    }

    private ModeListAdapter getDoubleSideAdapter() {
        return new ModeListAdapter(this, DisplayIdConverter.getYesNoStringIdArray(this, DOUBLE_SIDE_VALUE_LIST), 0);
    }

    private ModeListAdapter getTurnDirectionAdapter() {
        return new ModeListAdapter(this, (int[]) null, new int[]{R.drawable.scan_counter_direction, R.drawable.scan_same_direction}, 0);
    }

    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(PARAM_KEY_DOUBLE_SIDE, DOUBLE_SIDE_VALUE_LIST[mDoubleSideAdapter.getPosition()]);
        intent.putExtra(PARAM_KEY_TURN_DIRECTION, mTurnDirectionAdapter.getPosition());
        setResult(-1, intent);
        finish();
    }

    protected void onSaveInstanceState(Bundle bundle) {
        super.onSaveInstanceState(bundle);
        bundle.putBoolean(PARAM_KEY_DOUBLE_SIDE, DOUBLE_SIDE_VALUE_LIST[mDoubleSideAdapter.getPosition()].booleanValue());
        bundle.putInt(PARAM_KEY_TURN_DIRECTION, mTurnDirectionAdapter.getPosition());
    }

    public static Intent getStartIntent(Context context, boolean z, int i) {
        Intent intent = new Intent(context, I2DoubleSideScanSettingActivity.class);
        intent.putExtra(PARAM_KEY_DOUBLE_SIDE, z);
        intent.putExtra(PARAM_KEY_TURN_DIRECTION, i);
        return intent;
    }

    public static boolean getDoubleSideFromReturnIntent(Intent intent) {
        return intent != null && intent.getBooleanExtra(PARAM_KEY_DOUBLE_SIDE, false);
    }

    public static int getTurnDirectionFromReturnIntent(Intent intent) {
        if (intent == null) {
            return 0;
        }
        return intent.getIntExtra(PARAM_KEY_TURN_DIRECTION, 0);
    }
}
