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.ProjectInfoData;
import kr.co.hanwha.hifive.data.model.SpinnerData;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

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

    private int mSelectedProjectid = 0;

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

    public void setSelectedProjectid(int projectid) {
        this.mSelectedProjectid = projectid;
    }

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

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

    public void addItems(ArrayList<SpinnerData> list) {
        if(mListData == null) mListData = new ArrayList<>();
        else mListData.clear();

        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(getProjectName(position));
        int color = 0;
        if (mSelectedProjectid == getProjectId(position)) {
            color = HFCommonUtil.getColor(mContext, R.color.hf_blue02);
        } else {
            color = HFCommonUtil.getColor(mContext, R.color.hf_grey02);
        }
        textViewTitle.setTextColor(color);
        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(getProjectName(position));
        return view;
    }

    private String getProjectName(int index) {
        String name = "";
        if (index >= 0 && index < mListData.size() &&
                mListData.get(index).getValue() instanceof ProjectInfoData) {
            ProjectInfoData data = (ProjectInfoData) mListData.get(index).getValue();
            name = data.getProject_name();
        }
        return name;
    }

    public int getProjectId(int index) {
        int projectId = 0;
        if (index >= 0 && index < mListData.size() &&
                mListData.get(index).getValue() instanceof ProjectInfoData) {
            ProjectInfoData data = (ProjectInfoData) mListData.get(index).getValue();
            projectId = data.getProject_id();
        }
        return projectId;
    }


}
