package com.bernard.mcqinator.api.quiz;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.bernard.mcqinator.R;

/**
 * @author samy
 * @date on 17/04/17.
 */

public class DEVerbenQuiz extends FormQuiz{

    private static final int[] formNamesIDs = {R.string.infinitif,R.string.prasens2,R.string.prasens3,R.string.prateritum2,R.string.prateritum3,R.string.partizipPerfekt};

    public static final int AUX_HABEN = 0b0,AUX_SEIN = 0b1;


    @Override
    public String getBlocFrageText(int itemID, int formID, int fformID, BlocData frageData, Context context) {
        return context.getResources().getString(R.string.wasIstDasVon,context.getResources().getString(formNamesIDs[getItems()[itemID].getForms()[formID].getFormID()]),getItems()[itemID].getForms()[fformID].getRichtig(),context.getResources().getString(formNamesIDs[getItems()[itemID].getForms()[fformID].getFormID()]));
    }

    @Override
    public int getEditActivityLayoutID() {
        return R.layout.activity_edit_quiz_deverben;
    }

    @Override
    public int getEditItemActivityFormLayoutID() {
        return R.layout.edit_form_quiz_item_form;
    }

    @Override
    public int getEditItemActivityFalseLayoutID() {
        return R.layout.edit_form_quiz_item_false;
    }

    @Override
    public int getEditItemActivityLayoutID() {
        return R.layout.edit_form_quiz_item;
    }

    @Override
    public int getFormCount() {
        return formNamesIDs.length;
    }

    @Override
    protected View getEditQuizItemView(Item item, int position, View convertView, ViewGroup parent,Context c) {
        View group = (convertView == null)?LayoutInflater.from(c).inflate(R.layout.verbe_row_layout,parent,false):(ViewGroup)convertView;
        ((TextView)group.findViewById(R.id.verbeInfinitifText)).setText(item.getForms()[0].getRichtig());
        ((TextView)group.findViewById(R.id.verbeOthersText)).setText(item.getForms()[1].getRichtig()+item.getForms()[3].getRichtig()+item.getForms()[5].getRichtig());
        int ram = 0;
        for (Item.Form f : item.getForms())
            ram+=f.getFalshe().length + 1;
        ((TextView)group.findViewById(R.id.verbeInfinitifText)).setText(c.getResources().getQuantityString(R.plurals.worteCount,ram,ram));

        return group;
    }

    @Override
    public int getFormNameStringId(int pos) {
        return formNamesIDs[pos];
    }

    public class VerbeAdapter extends BaseAdapter{
        final Context ctx;

        public VerbeAdapter(Context ctx) {
            this.ctx = ctx;
        }

        @Override
        public int getCount() {
            return getItems().length;
        }

        @Override
        public Item getItem(int position) {
            return getItems()[position];
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            View layout = (convertView != null)?convertView: LayoutInflater.from(ctx).inflate(R.layout.verbe_row_layout,parent,false);
            TextView infinitif = layout.findViewById(R.id.verbeInfinitifText);
            TextView others = layout.findViewById(R.id.verbeOthersText);
            TextView worteCount = layout.findViewById(R.id.verbeWorteCountText);

            Item i = this.getItem(position);
            infinitif.setText(i.getForms()[0].getRichtig());
            others.setText(i.getForms()[3].getRichtig() + "," + i.getForms()[5].getRichtig() + "," + i.getForms()[6].getRichtig());
            int wC = i.getForms()[0].getFalshe().length + i.getForms()[1].getFalshe().length + i.getForms()[2].getFalshe().length + i.getForms()[3].getFalshe().length + i.getForms()[4].getFalshe().length + i.getForms()[5].getFalshe().length + i.getForms()[6].getFalshe().length;
            worteCount.setText(ctx.getResources().getQuantityString(R.plurals.worteCount,wC,wC));
            return null;
        }
    }
}