package com.bernard.mcqinator.view.activities;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.bernard.mcqinator.R;
import com.bernard.mcqinator.controller.QuizMetadataManager;
import com.bernard.mcqinator.view.TextBackgroundManager;
import com.bernard.mcqinator.view.QuizAdapter;
import com.bernard.mcqinator.api.QuizMetadata;

public class ChoosingQuizToEditActivity extends ListActivity {

    public static final String QUIZ_FILEID = "com.bernard.qcminator.quizFileID";

    TextBackgroundManager tbm;
    QuizAdapter qa;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_choosing_quiz_to_edit);
        tbm = new TextBackgroundManager(this);

        Button createQuizButton = findViewById(R.id.newQuizButton);
        createQuizButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(ChoosingQuizToEditActivity.this,CreateQuizActivity.class);
                //No data to transfer
                startActivity(i);
            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        qa = new QuizAdapter(QuizMetadataManager.getAllQuiz(this,true), this);
        setListAdapter(qa);
        tbm.stopTexts();
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        tbm.restartTexts();
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        QuizMetadata selected = qa.getItem(position);

        if(selected == null) {
            Log.e("EditQuiz","Trying to edit a null quiz ... aborting");
            Toast.makeText(this, "Cannot edit quiz", Toast.LENGTH_SHORT).show();
            return;
        }
        Intent i = new Intent(ChoosingQuizToEditActivity.this,selected.getType().getEditActivityClass());

        i.putExtra(ChoosingQuizToEditActivity.QUIZ_FILEID,selected.getFileID());

        startActivity(i);
    }
}
