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

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(TAG, "onCreate");
        setContentView((int) R.layout.copy_setting);
        Intent intent = getIntent();
        if (intent != null) {
            settingItemKey = intent.getStringExtra("Key");
            setActionBar(string(settingItemKey), true);
        }
        optionValueChangedListener = new ActivityBase.OptionItemChangedListener() {
            public void onOptionItemChanged(ECopyOptionItem eCopyOptionItem) {
                copyComponent.setCopyOptionItem(eCopyOptionItem);
            }
        };
        optionListener = new ECopyComponent.ICopyOptionListener() {
            public void onCopyOptionChanged(ECopyOptionItem eCopyOptionItem, ArrayList<ECopyOptionItem> arrayList, ECopyComponent.ICopyOptionListener.CopyOptionChangedError copyOptionChangedError) {
                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;
                }
                finish();
            }
        };
        optionContext = copyComponent.getBindedCopyOptionContext();
        copyComponent.bindCopyOptionContext(optionContext, optionListener);
        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.name().equalsIgnoreCase(settingItemKey)) {
                optionValueMap.put(key, new OthersettingValue(next));
            }
        }
    }

    class OthersettingValue extends ActivityBase.ListOptionValue {
        int selectedItemPos;

        OthersettingValue(ECopyOptionItem eCopyOptionItem) {
            super();
            bindOption(eCopyOptionItem);
            capabilitySetting(eCopyOptionItem, CopySettingActivity.this);
            setOptionValueChangedListener (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) findViewById(R.id.copy_setting_list);
            listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                    loading.show();
                    optionItem.selectChoice((ECopyOptionItem.ECopyOptionItemChoice) choices.get(i));
                    if  (changedListener != null) {
                        changedListener.onOptionItemChanged (optionItem);
                    }
                    isKeepSimpleAPConnection = true;
                }
            });
            listView.setAdapter(getArrayAdapter(this, context));
            for (int i = 0; i < choices.size(); i++) {
                if (selected == choices.get(i)) {
                    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);
                items = strArr;
                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[i]);
                if (i == 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() {
        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);
        }
    }
}
