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 java.util.ArrayList;
import java.util.Iterator;

import epson.print.R;
import epson.print.Util.EPLog;
import epson.print.copy.Component.ecopycomponent.ECopyComponent;
import epson.print.copy.Component.ecopycomponent.ECopyOptionItem;
import epson.print.widgets.LongTapRepeatAdapter;

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

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

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

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

        private void capabilitySetting(ECopyOptionItem eCopyOptionItem, Context context) {
            int i = 0;
            while (true) {
                if (i >= choices.size()) {
                    break;
                } else if (selected == choices.get(i)) {
                    selectedItemPos = i;
                    break;
                } else {
                    i++;
                }
            }
            LinearLayout linearLayout = (LinearLayout) findViewById(R.id.auto_fit);
            if (selectedItemPos == 0) {
                ((ImageView) findViewById(R.id.auto_select_icon)).setVisibility(0);
            } else {
                ((ImageView) 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);
                    optionItem.selectChoice (selected);
                    if  (changedListener != null) {
                        changedListener.onOptionItemChanged (optionItem);
                    }
                    isKeepSimpleAPConnection = true;
                    finish();
                }
            });
            LinearLayout linearLayout2 = (LinearLayout) findViewById(R.id.full_size);
            if (selectedItemPos == 1) {
                ((ImageView) findViewById(R.id.full_select_icon)).setVisibility(0);
            } else {
                ((ImageView) 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);
                    optionItem.selectChoice (selected);
                    if  (changedListener != null) {
                        changedListener.onOptionItemChanged (optionItem);
                    }
                    isKeepSimpleAPConnection = true;
                    finish();
                }
            });
            if (selectedItemPos == 2) {
                ((ImageView) findViewById(R.id.custom_select_icon)).setVisibility(0);
            } else {
                ((ImageView) findViewById(R.id.custom_select_icon)).setVisibility(8);
            }
            ListView listView = (ListView) 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);
                    optionItem.selectChoice (selected);
                    if  (changedListener != null) {
                        changedListener.onOptionItemChanged (optionItem);
                    }
                    isKeepSimpleAPConnection = true;
                    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);
                items = arrayList;
                inflater = (LayoutInflater) context.getSystemService(Service.LAYOUT_INFLATER_SERVICE);
            }

            public View getView(int i, View view, ViewGroup viewGroup) {
                if (view == null) {
                    view = inflater.inflate(R.layout.setting_item, (ViewGroup) null);
                }
                ((TextView) view.findViewById(R.id.setting_name)).setText(items.get(i).scalePresetName);
                int i2 = items.get(i).scaleNum;
                ((TextView) view.findViewById(R.id.setting_value)).setText(String.valueOf(i2) + " %");
                if (i == 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) {
                scalePresetName = string(eCopyOptionItemChoice.name());
                scaleNum = copyComponent.getCopyValue(eCopyType, eCopyOptionItemChoice);
            }
        }
    }

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

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

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

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

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

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

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

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

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