package epson.scan.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

import epson.print.ActivityIACommon;
import epson.print.R;

public class I2SettingDetailActivity extends ActivityIACommon {
    private static final String KEY_ADAPTOR = "adaptor";
    private static final String KEY_TITLE = "title";
    private static final String RESULT_KEY_SELECTED = "selected-position";

    protected void onCreate(Bundle bundle) {
        super.onCreate(bundle);
        setContentView(R.layout.activity_i2_setting_detail);
        setActionBar(getTitle().toString(), true);
        ListView listView = (ListView) findViewById(R.id.listView);
        Intent intent = getIntent();
        LocalListAdapterBuilder localListAdapterBuilder = (LocalListAdapterBuilder) intent.getSerializableExtra(KEY_ADAPTOR);
        if (localListAdapterBuilder != null) {
            ListAdapter build = localListAdapterBuilder.build(this);
            if (build != null) {
                listView.setAdapter(build);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long j) {
                        I2SettingDetailActivity.this.localItemSelected(i);
                    }
                });
                int intExtra = intent.getIntExtra(KEY_TITLE, 0);
                if (intExtra != 0) {
                    setTitle(intExtra);
                    return;
                }
                return;
            }
            throw new IllegalArgumentException();
        }
        throw new IllegalArgumentException("intent extra 'adaptor' == null.");
    }

    private void localItemSelected(int i) {
        Intent intent = new Intent();
        intent.putExtra(RESULT_KEY_SELECTED, i);
        setResult(-1, intent);
        finish();
    }

    public static Intent getStartIntent(Context context, LocalListAdapterBuilder localListAdapterBuilder, int i) {
        Intent intent = new Intent(context, I2SettingDetailActivity.class);
        intent.putExtra(KEY_ADAPTOR, localListAdapterBuilder);
        intent.putExtra(KEY_TITLE, i);
        return intent;
    }

    public static int getResultPosition(Intent intent) {
        if (intent == null) {
            return -1;
        }
        return intent.getIntExtra(RESULT_KEY_SELECTED, -1);
    }
}
