package com.bernard.mcqinator.view.activities.congratsActivity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Html;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.bernard.mcqinator.R;
import com.bernard.mcqinator.api.fragen.BlocFrage;

public class DEVerbenCongratsActivity extends CongratsActivity implements View.OnClickListener{

    ListView deverben;
    TextView score;
    Button doneButton;
    DEVerbenReplyadapter adapter;


    @Override
    public void create(@Nullable Bundle savedInstanceState) {
        setContentView(R.layout.activity_deverben_congrats);
        deverben = findViewById(R.id.verbeLayout);
        score = findViewById(R.id.noteText);
        int z = 0;
        for (int i = 0; i < replies.size(); i++)if(((BlocFrage.Reply) replies.get(i)).getAnswered().equals(((BlocFrage)fragen[i]).getRichtigAntwort()))z++;
        score.setText(z + "/" + replies.size());
        adapter = new DEVerbenReplyadapter();
        deverben.setAdapter(adapter);
        doneButton = findViewById(R.id.doneButton);
        doneButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    public class DEVerbenReplyadapter extends BaseAdapter{

        @Override
        public int getCount() {
            return replies.size();
        }

        @Override
        public Object getItem(int position) {
            return replies.get(position);
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewGroup lay;
            TextView text;
            if(convertView == null)
                lay = (ViewGroup)getLayoutInflater().inflate(R.layout.deverben_reply_row_layout,parent,false);
            else
                lay = (ViewGroup)convertView;
            text = lay.findViewById(R.id.deverbenRepyText);
            BlocFrage frage = (BlocFrage)fragen[position];
            BlocFrage.Reply reply = (BlocFrage.Reply) replies.get(position);
            boolean correct = reply.getAnswered().equals(frage.getRichtigAntwort());
            text.setGravity(Gravity.CENTER);
            if(correct){
                text.setText(frage.getRichtigAntwort());
                text.setBackgroundColor(getResources().getColor(R.color.goodAnswer));
            }else{
                text.setText(Html.fromHtml("<span style=\"text-decoration:line-through;\">" + reply.getAnswered() + "</span> -&gt; " + frage.getRichtigAntwort()));
                text.setBackgroundColor(getResources().getColor(R.color.wrongAnswer));
            }
            return lay;
        }
    }

}
