package epson.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.epson.iprint.shared.SharedParamScan;
import com.epson.lib.escani2.EscanI2Lib;
import com.epson.lib.escani2.ScanI2Params;
import epson.print.ActivityIACommon;
import epson.print.C2135R;
import epson.print.EPPrinterManager;
import epson.scan.activity.ModeListAdapter;
import epson.scan.activity.StringListAdapter;
import epson.scan.i2lib.I2ScanParamArbiter;
import epson.scan.i2lib.I2ScanParamManager;
import java.io.IOException;
import java.util.Locale;

public class I2ScanSettingActivity extends ActivityIACommon implements View.OnClickListener {
    private static final String PARAM_KEY_EXTERNAL_SCAN_PARAMS = "external-scan-params";
    private static final String PARAM_KEY_HIDE_COLOR_MODE = "hide-color-mode";
    private static final String PARAM_KEY_HIDE_RESOLUTION = "hide-resolution";
    private static final int REQUEST_CODE_CHANGE_ESC_I_VERSION = 100;
    private static final int REQUEST_CODE_SELECT_BRIGHTNESS = 8;
    private static final int REQUEST_CODE_SELECT_COLOR_MODE = 3;
    private static final int REQUEST_CODE_SELECT_DUPLEX = 5;
    private static final int REQUEST_CODE_SELECT_GAMMA = 6;
    private static final int REQUEST_CODE_SELECT_INPUT_UNIT = 1;
    private static final int REQUEST_CODE_SELECT_RESOLUTION = 4;
    private static final int REQUEST_CODE_SELECT_SCANNER = 7;
    private static final int REQUEST_CODE_SELECT_SCAN_SIZE = 2;
    private static final String RETURN_KEY_OLD_PREVIEW_VALID = "old-preview-valid";
    private View mBrightnessSeparator;
    private TextView mBrightnessText;
    private View mBrightnessViewGroup;
    private View mColorModeSeparator;
    private TextView mColorModeText;
    private View mColorModeViewGroup;
    private View mDuplexSeparator;
    private TextView mDuplexText;
    private View mDuplexViewGroup;
    private SharedParamScan mExternalScanParams;
    private TextView mGammaText;
    private View mGammaViewGroup;
    private boolean mHideColorMode;
    private boolean mHideResolutionMode;
    private I2ScanParamArbiter mI2ScanParamArbiter;
    private View mInputUnitSeparator;
    private TextView mInputUnitText;
    private View mInputUnitViewGroup;
    private ImageView mPrinterLocationIcon;
    private View mProgressBarLayout;
    private TextView mScanResolutionText;
    private View mScanResolutionViewGroup;
    private boolean mScanSizeSelected;
    private View mScanSizeSeparator;
    private TextView mScanSizeText;
    private View mScanSizeViewGroup;
    private boolean mScannerChanged;
    private TextView mScannerNameText;
    private View mScannerNameViewGroup;
    private ScannerPropertyWrapper mScannerPropertyWrapper;

    /* access modifiers changed from: protected */
    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        this.mI2ScanParamArbiter = new I2ScanParamArbiter();
        this.mScanSizeSelected = false;
        setContentView((int) C2135R.layout.activity_i2_scan_setting);
        setActionBar((int) C2135R.string.str_lbl_title_scan_settings, true);
        this.mScannerNameViewGroup = findViewById(C2135R.C2137id.llScannerName);
        this.mScannerNameViewGroup.setOnClickListener(this);
        this.mScannerNameText = (TextView) findViewById(C2135R.C2137id.tvScannerName);
        this.mPrinterLocationIcon = (ImageView) findViewById(C2135R.C2137id.imageViewPrinterLocation);
        this.mInputUnitViewGroup = findViewById(C2135R.C2137id.llScanSettingsSource);
        this.mInputUnitViewGroup.setOnClickListener(this);
        this.mInputUnitText = (TextView) findViewById(C2135R.C2137id.tvScannerSource);
        this.mScanSizeViewGroup = findViewById(C2135R.C2137id.llScanSettingsSize);
        this.mScanSizeViewGroup.setOnClickListener(this);
        this.mScanSizeText = (TextView) findViewById(C2135R.C2137id.tvScannerSize);
        this.mScanSizeSeparator = findViewById(C2135R.C2137id.scanSizeSeparator);
        this.mColorModeViewGroup = findViewById(C2135R.C2137id.llScanSettingsColor);
        this.mColorModeViewGroup.setOnClickListener(this);
        this.mColorModeText = (TextView) findViewById(C2135R.C2137id.tvScannerColor);
        this.mColorModeSeparator = findViewById(C2135R.C2137id.colorModeSeparator);
        this.mScanResolutionViewGroup = findViewById(C2135R.C2137id.llScanSettingsResolution);
        this.mScanResolutionViewGroup.setOnClickListener(this);
        this.mScanResolutionText = (TextView) findViewById(C2135R.C2137id.tvScannerResolution);
        this.mDuplexViewGroup = findViewById(C2135R.C2137id.llScanSettingsAdvance2Sided);
        this.mDuplexViewGroup.setOnClickListener(this);
        this.mDuplexText = (TextView) findViewById(C2135R.C2137id.tvScanner2Sided);
        this.mBrightnessViewGroup = findViewById(C2135R.C2137id.llScanSettingsDensity);
        this.mBrightnessViewGroup.setVisibility(8);
        this.mBrightnessViewGroup.setOnClickListener(this);
        this.mBrightnessText = (TextView) findViewById(C2135R.C2137id.tvScannerDensity);
        findViewById(C2135R.C2137id.brightnessSeparator).setVisibility(8);
        this.mGammaViewGroup = findViewById(C2135R.C2137id.llScanSettingsGamma);
        this.mGammaViewGroup.setOnClickListener(this);
        this.mGammaText = (TextView) findViewById(C2135R.C2137id.tvScannerGamma);
        this.mInputUnitSeparator = findViewById(C2135R.C2137id.viewScanSettingsSource);
        this.mDuplexSeparator = findViewById(C2135R.C2137id.viewScanSettingsAdvance2Sided);
        this.mBrightnessSeparator = findViewById(C2135R.C2137id.brightnessSeparator);
        this.mProgressBarLayout = findViewById(C2135R.C2137id.progressGetOption);
        setFieldFromIntent(getIntent());
        if (!readEscIVersionChangeIntent(getIntent())) {
            loadData();
        }
        updateScanParamDisplay();
    }

    private void setFieldFromIntent(@Nullable Intent intent) {
        if (intent == null) {
            this.mHideColorMode = false;
            this.mHideResolutionMode = false;
            this.mExternalScanParams = null;
        }
        this.mHideColorMode = intent.getBooleanExtra("hide-color-mode", false);
        this.mHideResolutionMode = intent.getBooleanExtra("hide-resolution", false);
        this.mExternalScanParams = (SharedParamScan) intent.getSerializableExtra("external-scan-params");
    }

    private boolean readEscIVersionChangeIntent(Intent intent) {
        if (intent == null) {
            return false;
        }
        this.mScannerPropertyWrapper = SettingActivityParams.getScannerPropertyWrapper(intent);
        ScannerPropertyWrapper scannerPropertyWrapper = this.mScannerPropertyWrapper;
        if (scannerPropertyWrapper == null || scannerPropertyWrapper.getEscIVersion() != 2) {
            return false;
        }
        this.mScannerChanged = true;
        this.mI2ScanParamArbiter.notifyScannerChanged(this, this.mScannerPropertyWrapper.getI2ScannerAllInfo());
        return true;
    }

    private void loadData() {
        this.mScannerPropertyWrapper = ScannerPropertyWrapper.load(this);
        this.mI2ScanParamArbiter.makeSelectionList(this, this.mScannerPropertyWrapper.getI2ScannerAllInfo(), I2ScanParamManager.loadScanI2Params(this));
    }

    private void hideProgress() {
        this.mProgressBarLayout.setVisibility(8);
    }

    private void updateScanParamDisplay() {
        updateScannerName();
        displayScannerLocationIcon();
        updateInputUnitDisplay();
        updateScanSize();
        updateImageType();
        updateResolution();
        updateDuplexDisplay();
        updateGammaDisplay();
        updateBrightnessDisplay();
        updateScanSizeSeparator();
    }

    private void updateScanSizeSeparator() {
        this.mScanSizeSeparator.setVisibility((this.mColorModeViewGroup.getVisibility() == 0 || this.mScanResolutionViewGroup.getVisibility() == 0) ? 0 : 8);
    }

    private void displayScannerLocationIcon() {
        String modelName = this.mScannerPropertyWrapper.getModelName();
        if (modelName == null || modelName.isEmpty()) {
            this.mPrinterLocationIcon.setVisibility(8);
            return;
        }
        this.mPrinterLocationIcon.setVisibility(0);
        int i = C2135R.C2136drawable.print_local;
        if (this.mScannerPropertyWrapper.getLocation() == 3) {
            i = C2135R.C2136drawable.print_ip;
        }
        this.mPrinterLocationIcon.setImageResource(i);
    }

    private void updateScannerName() {
        String ipPrinterUserName;
        if (this.mScannerPropertyWrapper.getLocation() != 3 || (ipPrinterUserName = new EPPrinterManager(this).getIpPrinterUserName(this.mScannerPropertyWrapper.getScannerId())) == null) {
            String modelName = this.mScannerPropertyWrapper.getModelName();
            if (modelName == null || modelName.isEmpty()) {
                this.mScannerNameText.setText(C2135R.string.str_lbl_title_scan);
            } else {
                this.mScannerNameText.setText(modelName);
            }
        } else {
            this.mScannerNameText.setText(ipPrinterUserName);
        }
    }

    private void updateInputUnitDisplay() {
        int i = this.mI2ScanParamArbiter.getInputUnitCandidates().length <= 1 ? 8 : 0;
        this.mInputUnitViewGroup.setVisibility(i);
        this.mInputUnitSeparator.setVisibility(i);
        this.mInputUnitText.setText(DisplayIdConverter.getInputUnitStringId(this.mI2ScanParamArbiter.getInputUnit()));
    }

    private void updateScanSize() {
        if (this.mScanSizeSelected) {
            this.mScanSizeText.setText(DisplayIdConverter.getScanSizeStringId(this.mI2ScanParamArbiter.getScanSize()));
            return;
        }
        this.mScanSizeText.setText(C2135R.string.str_settings_undefine_scansize);
    }

    private void updateImageType() {
        if (this.mHideColorMode) {
            this.mColorModeViewGroup.setVisibility(8);
            this.mColorModeSeparator.setVisibility(8);
            return;
        }
        this.mColorModeViewGroup.setVisibility(0);
        this.mColorModeSeparator.setVisibility(0);
        int colorStringId = DisplayIdConverter.getColorStringId(this.mI2ScanParamArbiter.getColorMode());
        if (colorStringId == 0) {
            colorStringId = C2135R.string.str_blank;
        }
        this.mColorModeText.setText(colorStringId);
    }

    private void updateResolution() {
        if (this.mHideResolutionMode) {
            this.mScanResolutionViewGroup.setVisibility(8);
            return;
        }
        this.mScanResolutionViewGroup.setVisibility(0);
        this.mScanResolutionText.setText(DisplayIdConverter.getScanResolutionStrings((Context) this, this.mI2ScanParamArbiter.getResolution().intValue()));
    }

    private void updateDuplexDisplay() {
        int i = this.mI2ScanParamArbiter.isDuplexSupported() ? 0 : 8;
        this.mDuplexSeparator.setVisibility(i);
        this.mDuplexViewGroup.setVisibility(i);
        this.mDuplexText.setText(DisplayIdConverter.getYesNoStringId(this, this.mI2ScanParamArbiter.getDuplex().booleanValue()));
    }

    private void updateBrightnessDisplay() {
        int i = this.mI2ScanParamArbiter.getColorMode() == EscanI2Lib.ColorMode.MONO_1BIT ? 0 : 8;
        SharedParamScan sharedParamScan = this.mExternalScanParams;
        if (sharedParamScan != null) {
            i = sharedParamScan.getScan_type() == 3 ? 0 : 8;
        }
        this.mBrightnessViewGroup.setVisibility(i);
        this.mBrightnessSeparator.setVisibility(i);
        if (i == 0) {
            this.mBrightnessText.setText(String.format(Locale.US, "%d", new Object[]{Integer.valueOf(255 - this.mI2ScanParamArbiter.getDensity())}));
        }
    }

    private void updateGammaDisplay() {
        this.mGammaText.setText(DisplayIdConverter.getGammaStringId(this.mI2ScanParamArbiter.getGamma()));
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(C2135R.C2138menu.menu_done, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != C2135R.C2137id.menuSettingsDone) {
            return super.onOptionsItemSelected(menuItem);
        }
        saveSettingsAndFinish();
        return true;
    }

    private void saveSettingsAndFinish() {
        try {
            ScanI2Params scanParam = this.mI2ScanParamArbiter.getScanParam();
            boolean isOldPreviewValid = isOldPreviewValid(this.mScannerPropertyWrapper, scanParam);
            I2ScanParamManager.saveParams(this, scanParam);
            this.mScannerPropertyWrapper.saveData(this, this.mScannerChanged);
            new EPPrinterManager(this).commitIPPrinterInfo();
            Intent intent = new Intent();
            intent.putExtra(RETURN_KEY_OLD_PREVIEW_VALID, isOldPreviewValid);
            setResult(-1, intent);
            finish();
        } catch (IOException unused) {
        }
    }

    private boolean isOldPreviewValid(ScannerPropertyWrapper scannerPropertyWrapper, ScanI2Params scanI2Params) {
        ScannerPropertyWrapper load = ScannerPropertyWrapper.load(this);
        if (scannerPropertyWrapper.getEscIVersion() != scannerPropertyWrapper.getEscIVersion() || !load.getModelName().equals(scannerPropertyWrapper.getModelName())) {
            return false;
        }
        ScanI2Params loadScanI2Params = I2ScanParamManager.loadScanI2Params(this);
        if (loadScanI2Params.inputUnit == scanI2Params.inputUnit && loadScanI2Params.colorMode == scanI2Params.colorMode && loadScanI2Params.resolutionMain == scanI2Params.resolutionMain && loadScanI2Params.resolutionSub == scanI2Params.resolutionSub && loadScanI2Params.duplexTurnDirection == scanI2Params.duplexTurnDirection && loadScanI2Params.duplex == scanI2Params.duplex && loadScanI2Params.manualAdfAlignment == scanI2Params.manualAdfAlignment && loadScanI2Params.density == scanI2Params.density && loadScanI2Params.userGamma == scanI2Params.userGamma) {
            return true;
        }
        return false;
    }

    public void onBackPressed() {
        new EPPrinterManager(this).rollbackIPPrinterInfo();
        super.onBackPressed();
    }

    public void onClick(View view) {
        int id = view.getId();
        if (id == C2135R.C2137id.llScannerName) {
            startSelectScannerActivity();
        } else if (id == C2135R.C2137id.llScanSettingsSource) {
            goInputUnitSelection();
        } else if (id == C2135R.C2137id.llScanSettingsSize) {
            goScanSizeSelection();
        } else if (id == C2135R.C2137id.llScanSettingsColor) {
            goImageTypeSelection();
        } else if (id == C2135R.C2137id.llScanSettingsResolution) {
            goScanResolutionSelection();
        } else if (id == C2135R.C2137id.llScanSettingsAdvance2Sided) {
            goDuplicateSelection();
        } else if (id == C2135R.C2137id.llScanSettingsDensity) {
            goBrightnessSelection();
        } else if (id == C2135R.C2137id.llScanSettingsGamma) {
            goGammaSelection();
        }
    }

    private void startSelectScannerActivity() {
        startActivityForResult(ScanSearchActivity.getStartIntent(this, this.mScannerPropertyWrapper.getScannerId(), this.mScannerPropertyWrapper.getLocation(), this.mScannerPropertyWrapper.getSimpleApSsid()), 7);
    }

    private void goImageTypeSelection() {
        startActivityForResult(I2SettingDetailActivity.getStartIntent(this, new ModeListAdapter.Builder(DisplayIdConverter.getColorModeItemIdArray(this.mI2ScanParamArbiter.getColorModeCandidates()), this.mI2ScanParamArbiter.getColorModePosition()), C2135R.string.str_settings_color_title), 3);
    }

    private void goInputUnitSelection() {
        startActivityForResult(InputUnitSettingActivity.getStartIntent(this, this.mI2ScanParamArbiter.getInputUnit() == EscanI2Lib.InputUnit.ADF ? 1 : 0, this.mI2ScanParamArbiter.isAdfAlignmentMenuValid() ? this.mI2ScanParamArbiter.getManualAdfAlignmentValue() : -1), 1);
    }

    private void goScanSizeSelection() {
        startActivityForResult(I2SettingDetailActivity.getStartIntent(this, new ModeListAdapter.Builder(DisplayIdConverter.getScanSizeIdArray(this.mI2ScanParamArbiter.getScanSizeCandidates()), this.mScanSizeSelected ? this.mI2ScanParamArbiter.getScanSizePosition() : -1), C2135R.string.str_scanning_size), 2);
    }

    private void goScanResolutionSelection() {
        startActivityForResult(I2SettingDetailActivity.getStartIntent(this, new StringListAdapter.Builder(DisplayIdConverter.getScanResolutionStrings((Context) this, this.mI2ScanParamArbiter.getResolutionCandidates()), this.mI2ScanParamArbiter.getResolutionPosition()), C2135R.string.str_settings_resolution_title), 4);
    }

    private void goDuplicateSelection() {
        startActivityForResult(I2DoubleSideScanSettingActivity.getStartIntent(this, this.mI2ScanParamArbiter.getDuplex().booleanValue(), this.mI2ScanParamArbiter.getDuplexTurnDirection()), 5);
    }

    private void goBrightnessSelection() {
        startActivityForResult(ScanSettingsAdvanceDensityActivity.getStartIntent(this, this.mI2ScanParamArbiter.getDensity(), this.mI2ScanParamArbiter.getDensityChangeable()), 8);
    }

    private void goGammaSelection() {
        startActivityForResult(I2SettingDetailActivity.getStartIntent(this, new ModeListAdapter.Builder(DisplayIdConverter.getGammaStringIdArray(this, this.mI2ScanParamArbiter.getGammaCandidates()), this.mI2ScanParamArbiter.getGammaPosition()), C2135R.string.str_settings_gamma_title), 6);
    }

    /* access modifiers changed from: protected */
    public void onActivityResult(int i, int i2, Intent intent) {
        if (i != 100) {
            switch (i) {
                case 1:
                    this.mI2ScanParamArbiter.setInputUnitPosition(InputUnitSettingActivity.getInputUnit(intent));
                    int adfAlignment = InputUnitSettingActivity.getAdfAlignment(intent);
                    if (adfAlignment != -1) {
                        this.mI2ScanParamArbiter.setManualAdfAlignmentValue(adfAlignment);
                    }
                    updateScanParamDisplay();
                    return;
                case 2:
                    if (i2 == -1 && intent != null) {
                        this.mScanSizeSelected = true;
                        this.mI2ScanParamArbiter.setScanSizePosition(I2SettingDetailActivity.getResultPosition(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                case 3:
                    if (i2 == -1 && intent != null) {
                        this.mI2ScanParamArbiter.setColorModePosition(I2SettingDetailActivity.getResultPosition(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                case 4:
                    if (i2 == -1 && intent != null) {
                        this.mI2ScanParamArbiter.setResolutionPosition(I2SettingDetailActivity.getResultPosition(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                case 5:
                    if (i2 == -1 && intent != null) {
                        this.mI2ScanParamArbiter.setDuplexValue(I2DoubleSideScanSettingActivity.getDoubleSideFromReturnIntent(intent));
                        this.mI2ScanParamArbiter.setDuplexTurnDirection(I2DoubleSideScanSettingActivity.getTurnDirectionFromReturnIntent(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                case 6:
                    if (i2 == -1 && intent != null) {
                        this.mI2ScanParamArbiter.setGammaPosition(I2SettingDetailActivity.getResultPosition(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                case 7:
                    onScannerSelectResult(i2, intent);
                    return;
                case 8:
                    if (i2 == -1 && intent != null) {
                        this.mI2ScanParamArbiter.setDensity(ScanSettingsAdvanceDensityActivity.getDensityFromReturnIntent(intent), ScanSettingsAdvanceDensityActivity.getChangeAble(intent));
                        updateScanParamDisplay();
                        return;
                    }
                    return;
                default:
                    return;
            }
        } else if (i2 == 1) {
            onScannerSelectResult(-1, intent);
        } else {
            setResult(i2, intent);
            finish();
        }
    }

    private void onScannerSelectResult(int i, Intent intent) {
        if (i != -1) {
            updateIfIpAddressPrinter();
            if (!this.mScannerPropertyWrapper.isPrinterSet()) {
                transitI1SettingActivity();
                return;
            }
            return;
        }
        this.mScannerChanged = true;
        this.mScanSizeSelected = false;
        this.mScannerPropertyWrapper = SettingActivityParams.getScannerPropertyWrapper(intent);
        ScannerPropertyWrapper scannerPropertyWrapper = this.mScannerPropertyWrapper;
        if (scannerPropertyWrapper == null) {
            transitI1SettingActivity();
        } else if (scannerPropertyWrapper.getEscIVersion() != 2) {
            transitI1SettingActivity();
        } else {
            this.mI2ScanParamArbiter.notifyScannerChanged(this, this.mScannerPropertyWrapper.getI2ScannerAllInfo());
            updateScanParamDisplay();
        }
    }

    private void transitI1SettingActivity() {
        if (SettingActivityParams.returnIfEscIVersionChanged(getIntent())) {
            setResult(1, SettingActivityParams.getSettingReturnIntent(this.mScannerPropertyWrapper));
            finish();
            return;
        }
        startActivityForResult(SettingActivityParams.getChangeSettingActivityStartIntent(this, this.mScannerPropertyWrapper, true, this.mHideColorMode, this.mHideResolutionMode, this.mExternalScanParams), 100);
    }

    private void updateIfIpAddressPrinter() {
        if (this.mScannerPropertyWrapper.getLocation() == 3 && !new EPPrinterManager(this).doseContainScannerIdInIpPrinterList(this.mScannerPropertyWrapper.getScannerId())) {
            this.mScannerPropertyWrapper.resetScanner();
            this.mI2ScanParamArbiter.tmpReset();
            updateScanParamDisplay();
        }
    }

    public static boolean getOldDataValidation(@Nullable Intent intent) {
        return intent == null || intent.getBooleanExtra(RETURN_KEY_OLD_PREVIEW_VALID, false);
    }

    @NonNull
    public static Intent getStartIntent(Context context, boolean z, boolean z2, @Nullable SharedParamScan sharedParamScan) {
        Intent intent = new Intent(context, I2ScanSettingActivity.class);
        intent.putExtra("hide-color-mode", z);
        intent.putExtra("hide-resolution", z2);
        intent.putExtra("external-scan-params", sharedParamScan);
        return intent;
    }
}
