package epson.print.copy;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
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 java.util.ArrayList;
import java.util.Iterator;

public class CopySettingActivity extends ActivityBase {
    private String TAG = "CopySettingActivity";
    ECopyComponent.ICopyOptionListener optionListener;
    ActivityBase.OptionItemChangedListener optionValueChangedListener;
    String settingItemKey;

    public void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        EPLog.i(this.TAG, "onCreate");
        setContentView((int) R.layout.copy_setting);
        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) {
                CopySettingActivity.this.copyComponent.setCopyOptionItem(eCopyOptionItem);
            }
        };
        this.optionListener = new ECopyComponent.ICopyOptionListener() {
            public void onCopyOptionChanged(ECopyOptionItem eCopyOptionItem, ArrayList<ECopyOptionItem> arrayList, ECopyComponent.ICopyOptionListener.CopyOptionChangedError copyOptionChangedError) {
                CopySettingActivity.this.loading.dismiss();
                if (copyOptionChangedError != null) {
                    CopySettingActivity copySettingActivity = CopySettingActivity.this;
                    ActivityBase.errorDialog errordialog = new ActivityBase.errorDialog(copySettingActivity);
                    String[] reasonText = errordialog.getReasonText(copyOptionChangedError);
                    errordialog.showErrorDialog(reasonText[0], reasonText[1]);
                    return;
                }
                CopySettingActivity.this.finish();
            }
        };
        this.optionContext = this.copyComponent.getBindedCopyOptionContext();
        this.copyComponent.bindCopyOptionContext(this.optionContext, this.optionListener);
        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.name().equalsIgnoreCase(this.settingItemKey)) {
                this.optionValueMap.put(key, new OthersettingValue(next));
            }
        }
    }

    class OthersettingValue extends ActivityBase.ListOptionValue {
        int selectedItemPos;

        OthersettingValue(ECopyOptionItem eCopyOptionItem) {
            super();
            bindOption(eCopyOptionItem);
            capabilitySetting(eCopyOptionItem, CopySettingActivity.this);
            setOptionValueChangedListener(CopySettingActivity.this.optionValueChangedListener);
        }

        private SettingAdapter getArrayAdapter(OthersettingValue othersettingValue, Context context) {
            return new SettingAdapter(context, R.layout.setting_item, othersettingValue.getKeyArray());
        }

        private void capabilitySetting(ECopyOptionItem eCopyOptionItem, Context context) {
            ListView listView = (ListView) CopySettingActivity.this.findViewById(R.id.copy_setting_list);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                    CopySettingActivity.this.loading.show();
                    OthersettingValue.this.optionItem.selectChoice((ECopyOptionItem.ECopyOptionItemChoice) OthersettingValue.this.choices.get(i));
                    if (OthersettingValue.this.changedListener != null) {
                        OthersettingValue.this.changedListener.onOptionItemChanged(OthersettingValue.this.optionItem);
                    }
                    CopySettingActivity.this.isKeepSimpleAPConnection = true;
                }
            });
            listView.setAdapter(getArrayAdapter(this, context));
            for (int i = 0; i < this.choices.size(); i++) {
                if (this.selected == this.choices.get(i)) {
                    this.selectedItemPos = i;
                    return;
                }
            }
        }

        private class SettingAdapter extends ArrayAdapter<String> {
            private LayoutInflater inflater;
            private String[] items;

            public SettingAdapter(Context context, int i, String[] strArr) {
                super(context, i, strArr);
                this.items = strArr;
                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[i]);
                if (i == OthersettingValue.this.selectedItemPos) {
                    ((ImageView) view.findViewById(R.id.setting_active_icon)).setVisibility(0);
                } else {
                    ((ImageView) view.findViewById(R.id.setting_active_icon)).setVisibility(8);
                }
                return view;
            }
        }
    }

    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);
        }
    }
}
