package com.cygnusa.hyperm.training.activities;

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

import com.cygnusa.hyperm.R;

public class CustomSpinnerAdapter extends BaseAdapter {

    private String[] resourceList;
    private Context mContext;
    private int gravity = Gravity.CENTER;

    public CustomSpinnerAdapter(String[] resourceList, Context mContext) {
        this.resourceList = resourceList;
        this.mContext = mContext;
    }

    public void updateList(String[] resourceList) {
        this.resourceList = resourceList;
        notifyDataSetChanged();
    }

    public void setGravity(int gravity) {
        this.gravity = gravity;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return resourceList == null ? 0 : resourceList.length;
    }

    @Override
    public Object getItem(int position) {
        return null;
    }

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

    @Override
    public View getDropDownView(int position, View view, ViewGroup parent) {
        if (view == null || !view.getTag().toString().equals("DROPDOWN")) {
            view = LayoutInflater.from(mContext).inflate(R.layout.class_selection, parent, false);
            view.setTag("DROPDOWN");
        }

        TextView textView = view.findViewById(android.R.id.text1);
        textView.setText(resourceList[position]);
        textView.setGravity(gravity);
        return view;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
            view = LayoutInflater.from(mContext).inflate(R.layout.
                    class_text, parent, false);
            view.setTag("NON_DROPDOWN");
        }
        TextView textView = view.findViewById(android.R.id.text1);
        textView.setText(resourceList[position]);
        textView.setGravity(gravity);
        return view;
    }

}