package com.bernard.mcqinator.view.quizEditing.formQuiz;

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

import com.bernard.mcqinator.R;
import com.bernard.mcqinator.api.QuizMetadata;
import com.bernard.mcqinator.api.quiz.FormQuiz;
import com.bernard.mcqinator.api.quiz.Quiz;
import com.bernard.mcqinator.controller.FileManager;
import com.bernard.mcqinator.controller.QuizMetadataManager;
import com.bernard.mcqinator.view.activities.ChoosingQuizToEditActivity;

import java.io.IOException;

public class EditFormQuiz extends ListActivity {

    public static final int ITEM_NEW = 0x5603B6BD;
    public static final int ITEM_CHANGE = 0x68E0B3A1;

    QuizMetadata quizMetadata;
    FormQuiz quiz;
    FormQuiz.ItemAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = getIntent();
        long fileID = i.getLongExtra(ChoosingQuizToEditActivity.QUIZ_FILEID,-1);//TODO check intent
        Quiz q;
        try {
            quizMetadata = QuizMetadataManager.getQuiz(this,fileID);
            q = FileManager.getQuiz(quizMetadata,this);
            if(q == null)throw new IOException("FileManager.getQuiz returned null");
        } catch (IOException e) {
            Log.e("EditingFormQuiz","Cannot instantiate the quiz or the quizmetadata",e);
            finish();
            return;
        }
        quiz = (FormQuiz)q;
        setContentView(quiz.getEditActivityLayoutID());
        try {
            Button neueItem = findViewById(R.id.neueItemButton);//TODO check button presence (not a try / catch)
            neueItem.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent i = new Intent(EditFormQuiz.this, EditFormQuizItem.class);
                    i.putExtra(EditFormQuizItem.TO_EDIT_ITEM, (Parcelable) null);
                    i.putExtra(EditFormQuizItem.QUIZ_FILEID, quizMetadata.getFileID());
                    startActivityForResult(i, ITEM_NEW);
                }
            });
        }catch(NullPointerException e){
            Log.e("EditFormQuiz","The layout "+quiz.getClass().getName()+".getEditActivityLayoutID() has to own a Button with the ID 'neueItemButton'");
        }catch(ClassCastException e){
            Log.e("EditFormQuiz","The view 'neueItemButton' in the layout "+quiz.getClass().getName()+".getEditActivityLayoutID() has to be a Button");
        }

        try {
            Button doneButton = findViewById(R.id.doneButton);//TODO check button presence (not a try / catch)
            doneButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    FileManager.saveQuiz(quizMetadata,quiz,EditFormQuiz.this);
                    finish();
                }
            });
        }catch(NullPointerException e){
            Log.d("EditFormQuiz","The layout "+quiz.getClass().getName()+".getEditActivityLayoutID() has to own a Button with the ID 'doneButton'");
        }catch(ClassCastException e){
            Log.d("EditFormQuiz","The view 'doneButton' in the layout "+quiz.getClass().getName()+".getEditActivityLayoutID() has to be a Button");
        }

        adapter = quiz.new ItemAdapter(this);
        setListAdapter(adapter);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == ITEM_NEW && resultCode == RESULT_OK){
            FormQuiz.Item v = data.getParcelableExtra(EditFormQuizItem.EDITED_ITEM);
            quiz.addItem(v);
            FileManager.saveQuiz(quizMetadata,quiz,this);
            adapter = quiz.new ItemAdapter(this);
            setListAdapter(adapter);
        }
        if(requestCode == ITEM_CHANGE && resultCode == RESULT_OK){
            FormQuiz.Item v = data.getParcelableExtra(EditFormQuizItem.EDITED_ITEM);
            int pos = data.getIntExtra(EditFormQuizItem.ITEM_POSITION,0);
            quiz.setItem(pos,v);
            FileManager.saveQuiz(quizMetadata,quiz,this);
            adapter = quiz.new ItemAdapter(this);
            setListAdapter(adapter);
        }
    }

    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent i = new Intent(EditFormQuiz.this, EditFormQuizItem.class);
        i.putExtra(EditFormQuizItem.TO_EDIT_ITEM,(Parcelable)adapter.getItem(position));
        i.putExtra(EditFormQuizItem.ITEM_POSITION,position);
        i.putExtra(EditFormQuizItem.QUIZ_FILEID,quizMetadata.getFileID());
        startActivityForResult(i,ITEM_CHANGE);
    }
}
