package kr.co.hanwha.hifive.adapter;

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 java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.data.model.SpinnerData;

/**
 * Created by sanghoonkwon on 2018. 2. 20..
 */

public class DefaultSpinnerAdapter extends BaseAdapter {
    private Context mContext = null;
    private LayoutInflater mInflater = null;
    private ArrayList<SpinnerData> mListData = new ArrayList<>();


    public DefaultSpinnerAdapter(Context context) {
        this.mContext = context;
        this.mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void clear() {
        mListData.clear();
    }

    public void addItem(SpinnerData item) {
        mListData.add(item);
    }

    public void addItems(ArrayList<SpinnerData> list) {
        mListData.addAll(list);
    }

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

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

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

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

        TextView textViewTitle = (TextView) view.findViewById(R.id.title_textView);
        textViewTitle.setText(getTitle(position));

        return view;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
            view = mInflater.inflate(R.layout.item_title_spinner, parent, false);
            view.setTag("NON_DROPDOWN");
        }
        TextView textViewTitle = (TextView) view.findViewById(R.id.title_textView);
        textViewTitle.setText(getTitle(position));
        return view;
    }

    private String getTitle(int position) {
        return position >= 0 && position < mListData.size() ? mListData.get(position).getStringValue() : "";
    }

}
