/**
 * Copyright (C) 2005-2012 XELOG AG
 */
package ch.spherIC.recurvebowsight.dialog;

import android.app.Dialog;

import android.content.Context;
import android.content.DialogInterface;

import android.os.Bundle;

import android.view.ContextThemeWrapper;
import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListView;

import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.RBSMainActivity;
import ch.spherIC.recurvebowsight.components.ArcerySetupArrayAdapter;
import ch.spherIC.recurvebowsight.database.dao.ArcherySetupDao;
import ch.spherIC.recurvebowsight.model.ArcherySetup;
import ch.spherIC.recurvebowsight.model.IArcherySetup;

import java.util.ArrayList;
import java.util.List;


/**
 * @author  FC Smilari
 */
public class ChooseArcherySetupDlg extends Dialog {

    private IArcherySetup archerySetup;

    /**
     * @param  context
     */
    public ChooseArcherySetupDlg(final Context context) {
        super(context);
        this.archerySetup = null;
    }


    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rbs_choose_archerysetup_dlg);
        setTitle(getContext().getResources().getText(R.string.menuChooseEquipment));

        setOnDismissListener(new OnDismissListener() {
                @Override
                public void onDismiss(final DialogInterface dialog) {
                    ((RBSMainActivity) ((ContextThemeWrapper) getContext()).getBaseContext()).onArcherySetupDlgDismissed(ChooseArcherySetupDlg.this.archerySetup);
                }
            });
    }

    @Override
    public void show() {
        super.show();

        final ListView listView = (ListView) findViewById(R.id.chooseArcerySetupItemsList);
        List<IArcherySetup> setups = new ArrayList<IArcherySetup>();
        this.archerySetup = null;

        setups.add(new ArcherySetup(-999, "", null, null, null, null, null, null, null, null, null, null, null, null, null, null, null, null));
        setups.addAll(ArcherySetupDao.getInstance().loadAll());

        listView.setAdapter(new ArcerySetupArrayAdapter(getContext(), R.layout.rbs_archerysetup_item, setups));
        listView.setOnItemClickListener(new OnItemClickListener() {
                @Override
                public void onItemClick(final AdapterView<?> parent, final View view, final int position,
                                        final long id) {
                    ChooseArcherySetupDlg.this.archerySetup = (IArcherySetup) listView.getItemAtPosition(position);
                    dismiss();
                }
            });
    }


    /**
     * Interface zum Listen von Dismiss-Events des ChooseArcherySetupDlg.
     *
     * @author  FC Smilari
     */
    public interface ChooseArcherySetupDlgDismissListener {
        void onArcherySetupDlgDismissed(IArcherySetup archerySetup);
    }
}
