package com.ericsson.uecontrol.gui;

import android.app.ListActivity;
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 com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.core.UeControlExecutor;
import com.ericsson.uecontrol.gui.fragments.ConfigureDialog;
import com.ericsson.uecontrol.gui.fragments.SelectBehaviourDialog;
import com.ericsson.uecontrol.gui.util.BehaviourListAdapter;
import com.ericsson.uecontrol.gui.util.DynamicListView;

import org.apache.log4j.Logger;

import java.io.File;
import java.util.List;

public class EditActivity extends ListActivity implements AdapterView.OnItemClickListener {
    private static final Logger log = Logger.getLogger(EditActivity.class);

    private UeControlExecutor executor;
    private BehaviourListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edit);

        final List<UeBehaviour> list;
        executor = MainActivity.getExecutor();
        list = executor.getBehaviourList();

        adapter = new BehaviourListAdapter(this, list);
        adapter.setEditable(true);
        DynamicListView listView = (DynamicListView) findViewById(android.R.id.list);
        listView.setAdapter(adapter);
        listView.setActionListener(adapter);
        listView.setOnItemClickListener(this);
        listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.edit, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_add) {
            SelectBehaviourDialog selector = new SelectBehaviourDialog();
            selector.setSelectionCallback(new SelectBehaviourDialog.BehaviourSelectionCallback() {
                public void behaviourSelected(String name, Class<UeBehaviour> objClass) {
                    try {
                        UeBehaviour obj = objClass.newInstance();
                        executor.addBehaviour(obj);
                        adapter.generateIds();
                        adapter.notifyDataSetChanged();
                    } catch (Exception e) {
                        log.warn(null, e);
                    }
                }
            });
            selector.show(getFragmentManager(), "behaviour_selector");
            return true;
        }
        else if (id == R.id.action_clear) {
            executor.getBehaviourList().clear();
            adapter.generateIds();
            adapter.notifyDataSetChanged();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
        UeBehaviour behaviour = executor.getBehaviourList().get(i);

        Bundle bundle = new Bundle();
        bundle.putInt("object_index", i);
        ConfigureDialog dialog = new ConfigureDialog();
        dialog.setArguments(bundle);
        dialog.show(getFragmentManager(), "behaviour_configurator");
    }

    @Override
    protected void onPause() {
        try {
            File input = new File(this.getFilesDir(), MainActivity.BEHAVIOUR_SAVE_FILE);
            executor.save(input.getAbsolutePath());
            log.debug("Saved current state");
            log.debug("Resetting executor");
            executor.reset();
        }catch(Exception e){log.error(null, e);}
        super.onPause();
    }

    @Override
    protected void onResume() {
        adapter.notifyDataSetChanged();
        super.onResume();
    }

}
