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.Button;

import com.bernard.mcqinator.R;
import com.bernard.mcqinator.controller.QuizMetadataManager;
import com.bernard.mcqinator.view.TextBackgroundManager;

public class MainActivity extends Activity {

    //TODO add a global javadoc
    //TODO add a global argument check with exceptions

    TextBackgroundManager tbm;
    public static final String PREFERENCES_FILE_KEY = "com.bernard.qcminator.SAXOPHONE";

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

        tbm = new TextBackgroundManager(this);

        final Button takeQuiz = findViewById(R.id.takeQuizButton);
        takeQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,ChoosingQuizActivity.class);
                //No data to transfer
                startActivity(i);
            }
        });
        final Button manageQuiz = findViewById(R.id.manageMyQuizButton);
        manageQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,QuizManagerActivity.class);
                //No data to transfer
                startActivity(i);
            }
        });
        final Button createQuiz = findViewById(R.id.createQuizButton);
        createQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(MainActivity.this,ChoosingQuizToEditActivity.class);
                //No data to transfer
                startActivity(i);
            }
        });

        final Button options = findViewById(R.id.optionsButton);
        createQuiz.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Intent i = new Intent(MainActivity.this,ChoosingQuizToEditActivity.class);
                //No data to transfer
                //startActivity(i);
            }
        });
        options.setEnabled(false);

        Thread t = new Thread(new Runnable() {
            @Override
            public void run() {
                QuizMetadataManager.retieveSavedQuizList(MainActivity.this);
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        findViewById(R.id.loading).setVisibility(View.GONE);
                        takeQuiz.setEnabled(true);
                        manageQuiz.setEnabled(true);
                        createQuiz.setEnabled(true);
                        //options.setEnabled(true);

                    }
                });
            }
        });
        t.start();

    }

    @Override
    protected void onStart() {
        super.onStart();
        tbm.stopTexts();
    }

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

    @Override
    protected void onDestroy() {
        super.onDestroy();
        QuizMetadataManager.saveSavedQuizList(this);
    }



}
