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);
        mScale = (RadioGroup) findViewById(R.id.document_size_scale);
        mScale.setOnCheckedChangeListener(this);
        mWidthText = (TextView) findViewById(R.id.width);
        mWidthDec = (Button) findViewById(R.id.width_dec_button);
        mWidthDec.setOnClickListener(this);
        mWidthInc = (Button) findViewById(R.id.width_inc_button);
        mWidthInc.setOnClickListener(this);
        mHeightText = (TextView) findViewById(R.id.height);
        mHeightDec = (Button) findViewById(R.id.height_dec_button);
        mHeightDec.setOnClickListener(this);
        mHeightInc = (Button) findViewById(R.id.height_inc_button);
        mHeightInc.setOnClickListener(this);
        LongTapRepeatAdapter.bless(mWidthDec);
        LongTapRepeatAdapter.bless(mWidthInc);
        LongTapRepeatAdapter.bless(mHeightDec);
        LongTapRepeatAdapter.bless(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) {
                mInch = true;
                convertDocumentSize();
            } else if (i == R.id.mm_button) {
                mInch = false;
                convertDocumentSize();
            }
        }
        radioGroup.invalidate();
    }

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

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

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

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

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

    private void convertDocumentSize() {
        if (mInch) {
            mWidth /= 25.4d;
            double d = mWidth;
            if (d < 0.1d) {
                d = 0.1d;
            }
            mWidth = d;
            double roundDownValue = getRoundDownValue(mWidth, 1);
            mWidthText.setText(String.valueOf(roundDownValue) + " in");
            mWidth = roundDownValue;
            mHeight = mHeight / 25.4d;
            double d2 = mHeight;
            if (d2 < 0.1d) {
                d2 = 0.1d;
            }
            mHeight = d2;
            double roundDownValue2 = getRoundDownValue(mHeight, 1);
            mHeightText.setText(String.valueOf(roundDownValue2) + " in");
            mHeight = roundDownValue2;
            return;
        }
        mWidth *= 25.4d;
        double d3 = mWidth;
        if (d3 > 1000.0d) {
            d3 = 1000.0d;
        }
        mWidth = d3;
        int roundDownValue3 = (int) getRoundDownValue(mWidth, 0);
        mWidthText.setText(String.valueOf(roundDownValue3) + " mm");
        mWidth = (double) roundDownValue3;
        mHeight = mHeight * 25.4d;
        double d4 = mHeight;
        if (d4 > 1000.0d) {
            d4 = 1000.0d;
        }
        mHeight = d4;
        int roundDownValue4 = (int) getRoundDownValue(mHeight, 0);
        mHeightText.setText(String.valueOf(roundDownValue4) + " mm");
        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;
        }
        mDocumentSizeInfo.setDocSizeName(obj);
        mDocumentSizeInfo.setScaleId(mInch ? 2 : 1);
        mDocumentSizeInfo.setWidth(getRoundDownValue(mWidth, 1));
        mDocumentSizeInfo.setHeight(getRoundDownValue(mHeight, 1));
        RegistedDocumentSizeList instance = RegistedDocumentSizeList.getInstance(this);
        if (instance.isExistDocumentSizeName(obj, mPosition)) {
            SimpleMessageDialogFragment.newInstance(R.string.exist_documentsize_message).show(getSupportFragmentManager(), "error-dialog");
            return;
        }
        int i = mPosition;
        if (i < 0) {
            instance.add(mDocumentSizeInfo);
            finish();
            return;
        }
        instance.update(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 (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);
        mWidthDec.dispatchTouchEvent(obtain);
        mWidthInc.dispatchTouchEvent(obtain);
        mHeightDec.dispatchTouchEvent(obtain);
        mHeightInc.dispatchTouchEvent(obtain);
    }

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