package ch.spherIC.recurvebowsight.configuration;

import android.app.Activity;
import android.app.Dialog;

import android.content.Intent;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import android.widget.AdapterView;
import android.widget.ListView;

import ch.spherIC.recurvebowsight.DialogFactory;
import ch.spherIC.recurvebowsight.R;
import ch.spherIC.recurvebowsight.RBSMainActivity;
import ch.spherIC.recurvebowsight.components.MainConfigurationArrayAdapter;
import ch.spherIC.recurvebowsight.utilities.CompatibilityUtil;

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


public class MainConfigurationActivity extends Activity {

    private ListView mainConfigListVIew;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.rbs_main_config);
        if (CompatibilityUtil.isTablet(getApplicationContext())) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        this.mainConfigListVIew = (ListView) findViewById(R.id.mainConfigListView);

        List<String[]> items = new ArrayList<String[]>();
        String[] riserItem = new String[] {
                                 getResources().getString(R.string.caption_MainCfg_Riser_MainText), getResources().getString(R.string.caption_MainCfg_Riser_SubText)
                             };
        String[] sightItem = new String[] {
                                 getResources().getString(R.string.caption_MainCfg_Sight_MainText), getResources().getString(R.string.caption_MainCfg_Sight_SubText)
                             };
        String[] archerySetupItem = new String[] {
                                        getResources().getString(R.string.caption_MainCfg_ArcerySetup_MainText), getResources().getString(R.string.caption_MainCfg_ArcerySetup_SubText)
                                    };
        items.add(riserItem);
        items.add(sightItem);
        items.add(archerySetupItem);

        final MainConfigurationArrayAdapter adapter = new MainConfigurationArrayAdapter(this, R.layout.rbs_main_config_item, items);

        this.mainConfigListVIew.setAdapter(adapter);
        this.mainConfigListVIew.setOnItemClickListener(new AdapterView.OnItemClickListener() {

                @Override
                public void onItemClick(final AdapterView<?> parent, final View view, final int position, final long id) {

                    final String[] item = (String[]) parent.getItemAtPosition(position);

                    if (item[0].equals(getResources().getString(R.string.caption_MainCfg_Riser_MainText))) {
                        Intent intent = new Intent(MainConfigurationActivity.this, RiserConfigurationActivity.class);
                        startActivityForResult(intent, 1);
                    } else if (item[0].equals(getResources().getString(R.string.caption_MainCfg_Sight_MainText))) {
                        Intent intent = new Intent(MainConfigurationActivity.this, SightConfigurationActivity.class);
                        startActivityForResult(intent, 1);
                    } else if (item[0].equals(getResources().getString(R.string.caption_MainCfg_ArcerySetup_MainText))) {
                        Intent intent = new Intent(MainConfigurationActivity.this, ArcherySetupConfigurationActivity.class);
                        startActivity(intent);
                    }
                }
            });
    }

    @Override
    public boolean onCreateOptionsMenu(final Menu menu) {
        getMenuInflater().inflate(R.menu.rbs_info_menu, menu);
        return true;
    }

    @Override
    protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        setResult(resultCode);
    }

    @Override
    public boolean onOptionsItemSelected(final MenuItem item) {

        // Handle item selection
        switch (item.getItemId()) {

        case R.id.itemAbout:
            showDialog(RBSMainActivity.DLG_ABOUT);
            return true;

        default:

            return super.onOptionsItemSelected(item);
        }
    }

    @Override
    protected Dialog onCreateDialog(final int id) {
        return DialogFactory.createDialog(id, this);
    }
}
