package com.bernard.mcqinator.view.activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;
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;

/*
    TODO make text "no installed quiz" diseapear
 */
public class CreateQuizActivity extends Activity implements AdapterView.OnItemSelectedListener{

    ArrayAdapter<QuizMetadata.Type> sadapter;
    TextBackgroundManager tbm;

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

        final EditText nameText = findViewById(R.id.quizNameAsk);
        final EditText authorText = findViewById(R.id.quizAuthorAsk);

        //Type select spiner
        final int defaultSelectIndex = 0;
        final Spinner sp = findViewById(R.id.quizTypeAsk);
        sadapter = new ArrayAdapter<>(this,android.R.layout.simple_spinner_item,QuizMetadata.Type.values());
        sadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        sp.setAdapter(sadapter);
        sp.setOnItemSelectedListener(this);
        sp.setSelection(defaultSelectIndex);

        //Parent quiz list
        final ListView lv = findViewById(R.id.quizParentChoose);
        QuizAdapter adapter = new QuizAdapter(QuizMetadataManager.getAllQuiz(this,sadapter.getItem(defaultSelectIndex)),this);
        lv.setAdapter(adapter);

        //Has parent checkbox
        final CheckBox hasParentBox = findViewById(R.id.quizHasParentAsk);
        hasParentBox.setSelected(false);
        hasParentBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                lv.setEnabled(isChecked);
            }
        });

        Button go = findViewById(R.id.doneButton);
        go.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QuizMetadata.Type quizType = (QuizMetadata.Type) sp.getSelectedItem();
                String name = nameText.getText().toString();
                if(name.isEmpty()){
                    Toast.makeText(CreateQuizActivity.this,R.string.quizNameEmpty,Toast.LENGTH_SHORT).show();
                    return;
                }
                String author = authorText.getText().toString();
                if(author.isEmpty()){
                    Toast.makeText(CreateQuizActivity.this,R.string.quizAuthorEmpty,Toast.LENGTH_SHORT).show();
                    return;
                }
                Intent i = new Intent(CreateQuizActivity.this,quizType.getEditActivityClass());

                QuizMetadata creation;

                if (hasParentBox.isChecked()) {
                    if(lv.getSelectedItem() == null){
                        Toast.makeText(CreateQuizActivity.this,R.string.quizParentEmpty,Toast.LENGTH_SHORT).show();
                        return;
                    }
                    QuizMetadata parent = (QuizMetadata) lv.getSelectedItem();
                    creation = QuizMetadataManager.createQuiz(CreateQuizActivity.this,name,author,true,parent);

                } else
                    creation = QuizMetadataManager.createQuiz(CreateQuizActivity.this, name, author, quizType, true);

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

                startActivity(i);
            }
        });
    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        ListView lv = findViewById(R.id.quizParentChoose);
        QuizAdapter adapter = new QuizAdapter(QuizMetadataManager.getAllQuiz(this,sadapter.getItem(position)),this);
        lv.setAdapter(adapter);
        TextView nothingText = findViewById(R.id.quizParentAskNoQuiz);
        if(adapter.getCount()<1){
            lv.setVisibility(View.GONE);
            nothingText.setVisibility(View.VISIBLE);
        }else{
            lv.setVisibility(View.VISIBLE);
            nothingText.setVisibility(View.GONE);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }
}
