package com.epson.cameracopy.p006ui;

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import com.epson.cameracopy.printlayout.DocumentSizeInfo;
import com.epson.cameracopy.printlayout.RegistedDocumentSizeList;
import epson.print.ActivityIACommon;

import epson.print.widgets.LongTapRepeatAdapter;
import java.math.BigDecimal;

/* renamed from: com.epson.cameracopy.ui.DocumentSizeEditActivity */
public class DocumentSizeEditActivity extends ActivityIACommon implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    private final double RATIO_INCHTOMM = 25.4d;
    private final double SIZE_INCH_MAX = 39.37d;
    private final double SIZE_INCH_MIN = 0.1d;
    private final int SIZE_MM_MAX = 1000;
    private final int SIZE_MM_MIN = 1;
    DocumentSizeInfo mDocumentSizeInfo;
    private double mHeight;
    private Button mHeightDec;
    private Button mHeightInc;
    private TextView mHeightText;
    private boolean mInch = false;
    int mPosition;
    private RadioGroup mScale;
    private double mWidth;
    private Button mWidthDec;
    private Button mWidthInc;
    private TextView mWidthText;

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView((int) R.layout.activity_document_size_edit);
        setActionBar((int) R.string.document_size_edit_title, true);
        this.mScale = (RadioGroup) findViewById(R.id.document_size_scale);
        this.mScale.setOnCheckedChangeListener(this);
        this.mWidthText = (TextView) findViewById(R.id.width);
        this.mWidthDec = (Button) findViewById(R.id.width_dec_button);
        this.mWidthDec.setOnClickListener(this);
        this.mWidthInc = (Button) findViewById(R.id.width_inc_button);
        this.mWidthInc.setOnClickListener(this);
        this.mHeightText = (TextView) findViewById(R.id.height);
        this.mHeightDec = (Button) findViewById(R.id.height_dec_button);
        this.mHeightDec.setOnClickListener(this);
        this.mHeightInc = (Button) findViewById(R.id.height_inc_button);
        this.mHeightInc.setOnClickListener(this);
        LongTapRepeatAdapter.bless(this.mWidthDec);
        LongTapRepeatAdapter.bless(this.mWidthInc);
        LongTapRepeatAdapter.bless(this.mHeightDec);
        LongTapRepeatAdapter.bless(this.mHeightInc);
        setDocumentSize();
    }

    protected void onPause() {
        super.onPause();
        if (isFinishing()) {
            deleteLongTapMessage();
        }
    }

    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.height_dec_button /*2131231100*/:
                decHeight();
                return;
            case R.id.height_inc_button /*2131231101*/:
                incHeight();
                return;
            case R.id.width_dec_button /*2131231633*/:
                decWidth();
                return;
            case R.id.width_inc_button /*2131231634*/:
                incWidth();
                return;
            default:
                return;
        }
    }

    public void onCheckedChanged(RadioGroup radioGroup, int i) {
        if (i != -1) {
            if (i == R.id.inch_button) {
                this.mInch = true;
                convertDocumentSize();
            } else if (i == R.id.mm_button) {
                this.mInch = false;
                convertDocumentSize();
            }
        }
        radioGroup.invalidate();
    }

    private void setDocumentSize() {
        this.mDocumentSizeInfo = (DocumentSizeInfo) getIntent().getParcelableExtra("DocumentSize");
        this.mPosition = getIntent().getIntExtra("DocumentSizePos", -1);
        ((EditText) findViewById(R.id.et_document_size)).setText(this.mDocumentSizeInfo.getDocSizeName(this));
        this.mInch = this.mDocumentSizeInfo.getScaleId() == 2;
        if (this.mInch) {
            this.mScale.check(R.id.inch_button);
            this.mWidth = this.mDocumentSizeInfo.getWidth();
            double roundDownValue = getRoundDownValue(this.mWidth, 1);
            TextView textView = this.mWidthText;
            textView.setText(String.valueOf(roundDownValue) + " in");
            this.mHeight = this.mDocumentSizeInfo.getHeight();
            double roundDownValue2 = getRoundDownValue(this.mHeight, 1);
            TextView textView2 = this.mHeightText;
            textView2.setText(String.valueOf(roundDownValue2) + " in");
            return;
        }
        this.mScale.check(R.id.mm_button);
        this.mWidth = this.mDocumentSizeInfo.getWidth();
        TextView textView3 = this.mWidthText;
        textView3.setText(String.valueOf((int) getRoundDownValue(this.mWidth, 0)) + " mm");
        this.mHeight = this.mDocumentSizeInfo.getHeight();
        TextView textView4 = this.mHeightText;
        textView4.setText(String.valueOf((int) getRoundDownValue(this.mHeight, 0)) + " mm");
    }

    private void decWidth() {
        if (this.mInch) {
            this.mWidth = decDouble(this.mWidth, 0.1d);
            if (this.mWidth < 0.1d) {
                this.mWidth = 0.1d;
            }
            double roundDownValue = getRoundDownValue(this.mWidth, 1);
            TextView textView = this.mWidthText;
            textView.setText(String.valueOf(roundDownValue) + " in");
            this.mWidth = roundDownValue;
            return;
        }
        this.mWidth = decDouble(this.mWidth, 1.0d);
        if (this.mWidth < 1.0d) {
            this.mWidth = 1.0d;
        }
        int roundDownValue2 = (int) getRoundDownValue(this.mWidth, 0);
        TextView textView2 = this.mWidthText;
        textView2.setText(String.valueOf(roundDownValue2) + " mm");
        this.mWidth = (double) roundDownValue2;
    }

    private void incWidth() {
        if (this.mInch) {
            this.mWidth = incDouble(this.mWidth, 0.1d);
            if (this.mWidth > 39.37d) {
                this.mWidth = 39.37d;
            }
            double roundDownValue = getRoundDownValue(this.mWidth, 1);
            TextView textView = this.mWidthText;
            textView.setText(String.valueOf(roundDownValue) + " in");
            this.mWidth = roundDownValue;
            return;
        }
        this.mWidth = incDouble(this.mWidth, 1.0d);
        if (this.mWidth > 1000.0d) {
            this.mWidth = 1000.0d;
        }
        int roundDownValue2 = (int) getRoundDownValue(this.mWidth, 0);
        TextView textView2 = this.mWidthText;
        textView2.setText(String.valueOf(roundDownValue2) + " mm");
        this.mWidth = (double) roundDownValue2;
    }

    private void decHeight() {
        if (this.mInch) {
            this.mHeight = decDouble(this.mHeight, 0.1d);
            if (this.mHeight < 0.1d) {
                this.mHeight = 0.1d;
            }
            double roundDownValue = getRoundDownValue(this.mHeight, 1);
            TextView textView = this.mHeightText;
            textView.setText(String.valueOf(roundDownValue) + " in");
            return;
        }
        this.mHeight = decDouble(this.mHeight, 1.0d);
        if (this.mHeight < 1.0d) {
            this.mHeight = 1.0d;
        }
        int roundDownValue2 = (int) getRoundDownValue(this.mHeight, 0);
        TextView textView2 = this.mHeightText;
        textView2.setText(String.valueOf(roundDownValue2) + " mm");
        this.mHeight = (double) roundDownValue2;
    }

    private void incHeight() {
        if (this.mInch) {
            this.mHeight = incDouble(this.mHeight, 0.1d);
            if (this.mHeight > 39.37d) {
                this.mHeight = 39.37d;
            }
            double roundDownValue = getRoundDownValue(this.mHeight, 1);
            TextView textView = this.mHeightText;
            textView.setText(String.valueOf(roundDownValue) + " in");
            this.mHeight = roundDownValue;
            return;
        }
        this.mHeight = incDouble(this.mHeight, 1.0d);
        if (this.mHeight > 1000.0d) {
            this.mHeight = 1000.0d;
        }
        int roundDownValue2 = (int) getRoundDownValue(this.mHeight, 0);
        TextView textView2 = this.mHeightText;
        textView2.setText(String.valueOf(roundDownValue2) + " mm");
        this.mHeight = (double) roundDownValue2;
    }

    private void convertDocumentSize() {
        if (this.mInch) {
            this.mWidth /= 25.4d;
            double d = this.mWidth;
            if (d < 0.1d) {
                d = 0.1d;
            }
            this.mWidth = d;
            double roundDownValue = getRoundDownValue(this.mWidth, 1);
            this.mWidthText.setText(String.valueOf(roundDownValue) + " in");
            this.mWidth = roundDownValue;
            this.mHeight = this.mHeight / 25.4d;
            double d2 = this.mHeight;
            if (d2 < 0.1d) {
                d2 = 0.1d;
            }
            this.mHeight = d2;
            double roundDownValue2 = getRoundDownValue(this.mHeight, 1);
            this.mHeightText.setText(String.valueOf(roundDownValue2) + " in");
            this.mHeight = roundDownValue2;
            return;
        }
        this.mWidth *= 25.4d;
        double d3 = this.mWidth;
        if (d3 > 1000.0d) {
            d3 = 1000.0d;
        }
        this.mWidth = d3;
        int roundDownValue3 = (int) getRoundDownValue(this.mWidth, 0);
        this.mWidthText.setText(String.valueOf(roundDownValue3) + " mm");
        this.mWidth = (double) roundDownValue3;
        this.mHeight = this.mHeight * 25.4d;
        double d4 = this.mHeight;
        if (d4 > 1000.0d) {
            d4 = 1000.0d;
        }
        this.mHeight = d4;
        int roundDownValue4 = (int) getRoundDownValue(this.mHeight, 0);
        this.mHeightText.setText(String.valueOf(roundDownValue4) + " mm");
        this.mHeight = (double) roundDownValue4;
    }

    private double getRoundDownValue(double d, int i) {
        return new BigDecimal(String.valueOf(d)).setScale(i, 1).doubleValue();
    }

    private void checkDocumentSize() {
        String obj = ((EditText) findViewById(R.id.et_document_size)).getText().toString();
        if (obj.isEmpty()) {
            SimpleMessageDialogFragment.newInstance(R.string.no_name_documentsize_message).show(getSupportFragmentManager(), "error-dialog");
            return;
        }
        this.mDocumentSizeInfo.setDocSizeName(obj);
        this.mDocumentSizeInfo.setScaleId(this.mInch ? 2 : 1);
        this.mDocumentSizeInfo.setWidth(getRoundDownValue(this.mWidth, 1));
        this.mDocumentSizeInfo.setHeight(getRoundDownValue(this.mHeight, 1));
        RegistedDocumentSizeList instance = RegistedDocumentSizeList.getInstance(this);
        if (instance.isExistDocumentSizeName(obj, this.mPosition)) {
            SimpleMessageDialogFragment.newInstance(R.string.exist_documentsize_message).show(getSupportFragmentManager(), "error-dialog");
            return;
        }
        int i = this.mPosition;
        if (i < 0) {
            instance.add(this.mDocumentSizeInfo);
            finish();
            return;
        }
        instance.update(this.mDocumentSizeInfo, i);
        finish();
    }

    private double incDouble(double d, double d2) {
        return new BigDecimal(String.valueOf(d)).add(new BigDecimal(String.valueOf(d2))).doubleValue();
    }

    private double decDouble(double d, double d2) {
        return new BigDecimal(String.valueOf(d)).subtract(new BigDecimal(String.valueOf(d2))).doubleValue();
    }

    public boolean onCreateOptionsMenu(Menu menu) {
        if (this.mDocumentSizeInfo.getPaperId() == -2) {
            getMenuInflater().inflate(R.C2138menu.menu_done, menu);
        }
        return super.onCreateOptionsMenu(menu);
    }

    public boolean onOptionsItemSelected(MenuItem menuItem) {
        if (menuItem.getItemId() != R.id.menuSettingsDone) {
            return super.onOptionsItemSelected(menuItem);
        }
        checkDocumentSize();
        return true;
    }

    protected void deleteLongTapMessage() {
        MotionEvent obtain = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 10, 1, 0.0f, 0.0f, 0);
        this.mWidthDec.dispatchTouchEvent(obtain);
        this.mWidthInc.dispatchTouchEvent(obtain);
        this.mHeightDec.dispatchTouchEvent(obtain);
        this.mHeightInc.dispatchTouchEvent(obtain);
    }

    public void onConfigurationChanged(Configuration configuration) {
        super.onConfigurationChanged(configuration);
        deleteLongTapMessage();
    }
}
