package epson.print.copy;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import com.epson.mobilephone.common.wifidirect.WiFiDirectManager;
import epson.print.R;
import epson.print.Util.EPLog;
import epson.print.copy.ActivityBase;
import epson.print.copy.Component.ecopycomponent.ECopyComponent;
import epson.print.copy.Component.ecopycomponent.ECopyOptionItem;
import epson.print.widgets.LongTapRepeatAdapter;
import java.util.ArrayList;
import java.util.Iterator;

public class CopyScaleActivity extends ActivityBase {
    private String TAG = "CppyScaleActivity";
    ActivityBase.OptionItemChangedListener optionValueChangedListener;
    String settingItemKey;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EPLog.i(this.TAG, "onCreate");
        setContentView((int) R.layout.copy_scale);
        Intent intent = getIntent();
        if (intent != null) {
            this.settingItemKey = intent.getStringExtra("Key");
            setActionBar(string(this.settingItemKey), true);
        }
        this.optionValueChangedListener = new ActivityBase.OptionItemChangedListener() {
            public void onOptionItemChanged(ECopyOptionItem eCopyOptionItem) {
                ArrayList arrayList = new ArrayList();
                CopyScaleActivity.this.copyComponent.setCopyOptionItem(eCopyOptionItem);
                if (eCopyOptionItem.getKey() == ECopyOptionItem.ECopyOptionItemKey.CopyMagnification) {
                    arrayList.add(eCopyOptionItem);
                    CopyScaleActivity.this.buildCopyOptions(arrayList);
                }
            }
        };
        buildCopyOptions(this.copyComponent.getCopyOptionItems());
    }

    private void buildCopyOptions(ArrayList<ECopyOptionItem> arrayList) {
        EPLog.i(this.TAG, "buildCopyOptions");
        Iterator<ECopyOptionItem> it = arrayList.iterator();
        while (it.hasNext()) {
            ECopyOptionItem next = it.next();
            ECopyOptionItem.ECopyOptionItemKey key = next.getKey();
            if (key == ECopyOptionItem.ECopyOptionItemKey.CopyMagnification) {
                this.optionValueMap.put(key, new CopyMagnificationValue(next));
            }
            if (key.name().equalsIgnoreCase(this.settingItemKey)) {
                this.optionValueMap.put(key, new XScaleValue(next));
            }
        }
    }

    class XScaleValue extends ActivityBase.ListOptionValue {
        final int AUTOFIT = 0;
        final int CUSTOM = 2;
        final int FULLSIZE = 1;
        ArrayList<ScaleInfo> presetArray = new ArrayList<>();
        int selectedItemPos;

        XScaleValue(ECopyOptionItem eCopyOptionItem) {
            super();
            bindOption(eCopyOptionItem);
            capabilitySetting(eCopyOptionItem, CopyScaleActivity.this);
            setOptionValueChangedListener(CopyScaleActivity.this.optionValueChangedListener);
        }

        private ScaleAdapter getArrayAdapter(XScaleValue xScaleValue, Context context) {
            for (int i = 3; i < xScaleValue.getKeyArray().length; i++) {
                this.presetArray.add(new ScaleInfo(CopyScaleActivity.this.copyType, (ECopyOptionItem.ECopyOptionItemChoice) xScaleValue.choices.get(i)));
            }
            return new ScaleAdapter(context, R.layout.setting_item, this.presetArray);
        }

        private void capabilitySetting(ECopyOptionItem eCopyOptionItem, Context context) {
            int i = 0;
            while (true) {
                if (i >= this.choices.size()) {
                    break;
                } else if (this.selected == this.choices.get(i)) {
                    this.selectedItemPos = i;
                    break;
                } else {
                    i++;
                }
            }
            LinearLayout linearLayout = (LinearLayout) CopyScaleActivity.this.findViewById(R.id.auto_fit);
            if (this.selectedItemPos == 0) {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.auto_select_icon)).setVisibility(0);
            } else {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.auto_select_icon)).setVisibility(8);
            }
            linearLayout.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    XScaleValue xScaleValue = XScaleValue.this;
                    xScaleValue.selected = (ECopyOptionItem.ECopyOptionItemChoice) xScaleValue.choices.get(0);
                    XScaleValue.this.optionItem.selectChoice(XScaleValue.this.selected);
                    if (XScaleValue.this.changedListener != null) {
                        XScaleValue.this.changedListener.onOptionItemChanged(XScaleValue.this.optionItem);
                    }
                    CopyScaleActivity.this.isKeepSimpleAPConnection = true;
                    CopyScaleActivity.this.finish();
                }
            });
            LinearLayout linearLayout2 = (LinearLayout) CopyScaleActivity.this.findViewById(R.id.full_size);
            if (this.selectedItemPos == 1) {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.full_select_icon)).setVisibility(0);
            } else {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.full_select_icon)).setVisibility(8);
            }
            linearLayout2.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    XScaleValue xScaleValue = XScaleValue.this;
                    xScaleValue.selected = (ECopyOptionItem.ECopyOptionItemChoice) xScaleValue.choices.get(1);
                    XScaleValue.this.optionItem.selectChoice(XScaleValue.this.selected);
                    if (XScaleValue.this.changedListener != null) {
                        XScaleValue.this.changedListener.onOptionItemChanged(XScaleValue.this.optionItem);
                    }
                    CopyScaleActivity.this.isKeepSimpleAPConnection = true;
                    CopyScaleActivity.this.finish();
                }
            });
            if (this.selectedItemPos == 2) {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.custom_select_icon)).setVisibility(0);
            } else {
                ((ImageView) CopyScaleActivity.this.findViewById(R.id.custom_select_icon)).setVisibility(8);
            }
            ListView listView = (ListView) CopyScaleActivity.this.findViewById(R.id.scale_list);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                    XScaleValue xScaleValue = XScaleValue.this;
                    xScaleValue.selected = (ECopyOptionItem.ECopyOptionItemChoice) xScaleValue.choices.get(i + 3);
                    XScaleValue.this.optionItem.selectChoice(XScaleValue.this.selected);
                    if (XScaleValue.this.changedListener != null) {
                        XScaleValue.this.changedListener.onOptionItemChanged(XScaleValue.this.optionItem);
                    }
                    CopyScaleActivity.this.isKeepSimpleAPConnection = true;
                    CopyScaleActivity.this.finish();
                }
            });
            listView.setAdapter(getArrayAdapter(this, context));
        }

        private class ScaleAdapter extends ArrayAdapter<ScaleInfo> {
            private LayoutInflater inflater;
            private ArrayList<ScaleInfo> items;

            public ScaleAdapter(Context context, int i, ArrayList<ScaleInfo> arrayList) {
                super(context, i, arrayList);
                this.items = arrayList;
                this.inflater = (LayoutInflater) context.getSystemService("layout_inflater");
            }

            public View getView(int i, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = this.inflater.inflate(R.layout.setting_item, (ViewGroup) null);
                }
                ((TextView) view.findViewById(R.id.setting_name)).setText(this.items.get(i).scalePresetName);
                int i2 = this.items.get(i).scaleNum;
                ((TextView) view.findViewById(R.id.setting_value)).setText(String.valueOf(i2) + " %");
                if (i == XScaleValue.this.selectedItemPos - 3) {
                    ((ImageView) view.findViewById(R.id.setting_active_icon)).setVisibility(0);
                } else {
                    ((ImageView) view.findViewById(R.id.setting_active_icon)).setVisibility(8);
                }
                return view;
            }
        }

        private class ScaleInfo {
            int scaleNum;
            String scalePresetName;

            ScaleInfo(ECopyComponent.ECopyType eCopyType, ECopyOptionItem.ECopyOptionItemChoice eCopyOptionItemChoice) {
                this.scalePresetName = CopyScaleActivity.this.string(eCopyOptionItemChoice.name());
                this.scaleNum = CopyScaleActivity.this.copyComponent.getCopyValue(eCopyType, eCopyOptionItemChoice);
            }
        }
    }

    class CopyMagnificationValue extends ActivityBase.NumberOptionValue {
        CopyMagnificationValue(ECopyOptionItem eCopyOptionItem) {
            super();
            bindOption(R.id.CopyMagnification, eCopyOptionItem);
            clickCustomScale();
            setOptionValueChangedListener(CopyScaleActivity.this.optionValueChangedListener);
        }

        private void bindOption(int i, ECopyOptionItem eCopyOptionItem) {
            this.optionItem = eCopyOptionItem;
            this.editText = (TextView) CopyScaleActivity.this.findViewById(i);
            this.value = eCopyOptionItem.getSelectedValue();
            if (this.value == ECopyOptionItem.CopyMagnificationAutofitValue) {
                this.editText.setText("---");
            } else {
                this.editText.setText(String.valueOf(this.value));
            }
            this.editText.setEnabled(eCopyOptionItem.isEnabled());
        }

        private void clickCustomScale() {
            ((LinearLayout) CopyScaleActivity.this.findViewById(R.id.copy_scale_custom)).setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) {
                    View inflate = LayoutInflater.from(CopyScaleActivity.this).inflate(R.layout.copy_custom_scale, (ViewGroup) null);
                    CopyMagnificationValue.this.editText = (TextView) inflate.findViewById(R.id.SettingCopyMagnification);
                    CopyMagnificationValue copyMagnificationValue = CopyMagnificationValue.this;
                    copyMagnificationValue.value = copyMagnificationValue.optionItem.getSelectedValue();
                    if (CopyMagnificationValue.this.value == ECopyOptionItem.CopyMagnificationAutofitValue) {
                        CopyMagnificationValue.this.value = 100;
                    }
                    CopyMagnificationValue.this.editText.setText(String.valueOf(CopyMagnificationValue.this.value));
                    CopyMagnificationValue.this.countUp = (Button) inflate.findViewById(R.id.SettingButonCopyMagnificationCountUp);
                    CopyMagnificationValue.this.countUp.setOnClickListener(new CountCustomScale(1));
                    if (CopyMagnificationValue.this.value == CopyMagnificationValue.this.optionItem.getMaximumValue()) {
                        CopyMagnificationValue.this.countUp.setEnabled(false);
                    } else {
                        CopyMagnificationValue.this.countUp.setEnabled(true);
                    }
                    LongTapRepeatAdapter.bless(CopyMagnificationValue.this.countUp);
                    CopyMagnificationValue.this.countDown = (Button) inflate.findViewById(R.id.SettingButonCopyMagnificationCountDown);
                    CopyMagnificationValue.this.countDown.setOnClickListener(new CountCustomScale(-1));
                    if (CopyMagnificationValue.this.value == CopyMagnificationValue.this.optionItem.getMinimumValue()) {
                        CopyMagnificationValue.this.countDown.setEnabled(false);
                    } else {
                        CopyMagnificationValue.this.countDown.setEnabled(true);
                    }
                    LongTapRepeatAdapter.bless(CopyMagnificationValue.this.countDown);
                    AlertDialog.Builder builder = new AlertDialog.Builder(CopyScaleActivity.this);
                    builder.setTitle(R.string.XScale_Custom);
                    builder.setView(inflate);
                    builder.setPositiveButton(CopyScaleActivity.this.getString(R.string.str_ok), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            ActivityBase.ListOptionValue listOptionValue = (ActivityBase.ListOptionValue) CopyScaleActivity.this.optionValueMap.get(ECopyOptionItem.ECopyOptionItemKey.XScale);
                            listOptionValue.optionItem.selectChoice(ECopyOptionItem.ECopyOptionItemChoice.XScale_Custom);
                            if (CopyMagnificationValue.this.changedListener != null) {
                                CopyMagnificationValue.this.changedListener.onOptionItemChanged(listOptionValue.optionItem);
                            }
                            CopyMagnificationValue.this.optionItem.selectValue(Integer.parseInt(CopyMagnificationValue.this.editText.getText().toString()));
                            if (CopyMagnificationValue.this.changedListener != null) {
                                CopyMagnificationValue.this.changedListener.onOptionItemChanged(CopyMagnificationValue.this.optionItem);
                            }
                            CopyScaleActivity.this.isKeepSimpleAPConnection = true;
                            CopyMagnificationValue.this.onDismissDialog();
                            CopyScaleActivity.this.finish();
                        }
                    });
                    builder.setNegativeButton(CopyScaleActivity.this.getString(R.string.str_cancel), new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialogInterface, int i) {
                            CopyMagnificationValue.this.onDismissDialog();
                        }
                    });
                    builder.show();
                    CopyMagnificationValue copyMagnificationValue2 = CopyMagnificationValue.this;
                    copyMagnificationValue2.setOptionValueChangedListener(CopyScaleActivity.this.optionValueChangedListener);
                }
            });
        }

        private void onDismissDialog() {
            MotionEvent obtain = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis() + 10, 1, 0.0f, 0.0f, 0);
            this.countDown.dispatchTouchEvent(obtain);
            this.countUp.dispatchTouchEvent(obtain);
        }

        class CountCustomScale extends ActivityBase.NumberOptionValue.Counter {
            public CountCustomScale(int i) {
                super(i);
            }

            public void onClick(View view) {
                try {
                    CopyMagnificationValue.this.value = Integer.valueOf(CopyMagnificationValue.this.editText.getText().toString()).intValue();
                    CopyMagnificationValue.this.value = Math.min(Math.max(CopyMagnificationValue.this.optionItem.getMinimumValue(), CopyMagnificationValue.this.value + this.amount), CopyMagnificationValue.this.optionItem.getMaximumValue());
                } catch (NumberFormatException unused) {
                }
                CopyMagnificationValue.this.editText.setText(String.valueOf(CopyMagnificationValue.this.value));
                if (CopyMagnificationValue.this.value >= CopyMagnificationValue.this.optionItem.getMaximumValue()) {
                    CopyMagnificationValue.this.countUp.setEnabled(false);
                } else {
                    CopyMagnificationValue.this.countUp.setEnabled(true);
                }
                if (CopyMagnificationValue.this.value <= CopyMagnificationValue.this.optionItem.getMinimumValue()) {
                    CopyMagnificationValue.this.countDown.setEnabled(false);
                } else {
                    CopyMagnificationValue.this.countDown.setEnabled(true);
                }
            }
        }
    }

    public void onBackPressed() {
        this.isKeepSimpleAPConnection = true;
        finish();
    }

    protected void onResume() {
        super.onResume();
        if (!WiFiDirectManager.isNeedConnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER)) {
            this.isTryConnectSimpleAp = false;
        } else if (!this.isTryConnectSimpleAp) {
            this.isTryConnectSimpleAp = true;
            if (WiFiDirectManager.reconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, -1)) {
                this.isKeepSimpleAPConnection = true;
            }
        }
    }

    protected void onPause() {
        super.onPause();
        if (!this.isKeepSimpleAPConnection) {
            WiFiDirectManager.disconnect(this, WiFiDirectManager.DEVICE_TYPE_PRINTER, ActivityBase.printerIp);
        }
    }
}
