package kr.co.hanwha.hifive.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.ViewGroup;

import java.util.ArrayList;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.adapter.contract.MainDashboardListContract;
import kr.co.hanwha.hifive.adapter.viewholder.MainDashboardGridViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.data.model.DabProjectData;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by sanghoonkwon on 2018. 3. 15..
 */

public class MainDashboardListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
        implements MainDashboardListContract.Model, MainDashboardListContract.View {
    private Context mContext;

    private final int TYPE_PROJECT = 0;
    private final int TYPE_TOP_MARGIN = 1;

    private final int COUNT_TOP_MARGIN = 1;
    private final int ZERO = 0;
    private int mCellWidth = 600;
    private int mTopMarginHeight = 30;

    private MainDashboardListContract.OnMainDashboardListListener mOnMainDashboardListListener;
    private ArrayList<DabProjectData> mListData;

    public MainDashboardListAdapter(@NonNull Context context,
                                    @NonNull MainDashboardListContract.OnMainDashboardListListener onMainDashboardListListener) {
        mContext = context;
        mOnMainDashboardListListener = onMainDashboardListListener;

        int screenWidth = HFCommonUtil.getScreenWidth(context);
        int dp10 = HFCommonUtil.getPixelFromDimension(context, R.dimen.dimen_dp10);
        mCellWidth = (screenWidth - dp10 * 6) / 2;
        mTopMarginHeight = dp10 * 3;

    }

    /**************************************************
     * View
     **************************************************/
    @Override
    public void notifyDataAll() {
        this.notifyDataSetChanged();
    }

    @Override
    public void notifyDataRangeChanged(int positionStart, int itemCount) {
        this.notifyItemRangeChanged(positionStart, itemCount);
    }

    /**************************************************
     * Model
     **************************************************/

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

        if (list != null)
            mListData.addAll(list);
    }

    /**************************************************
     * Adapter
     **************************************************/
    public boolean getFullSpan(int position) {
        if (position == 0) return true;
        else return false;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0)
            return TYPE_TOP_MARGIN;
        else return TYPE_PROJECT;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_TOP_MARGIN) {
            return new MarginViewHolder(parent);
        } else if (viewType == TYPE_PROJECT) {
            return new MainDashboardGridViewHolder(parent, mOnMainDashboardListListener);
        }

        throw new RuntimeException("############################################################\n" +
                "There is no type that matches the type " + viewType + " + make sure your using types correctly\n" +
                "############################################################");
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == TYPE_TOP_MARGIN) {
            MarginViewHolder view = (MarginViewHolder) holder;
            view.onBind(null, mTopMarginHeight);
        } else if (getItemViewType(position) == TYPE_PROJECT) {
            int index = position - COUNT_TOP_MARGIN;
            DabProjectData data = mListData.get(index);
            MainDashboardGridViewHolder view = (MainDashboardGridViewHolder) holder;
            view.onBind(data, mCellWidth);
        }
    }

    @Override
    public int getItemCount() {
        return (mListData != null ? mListData.size() + COUNT_TOP_MARGIN : ZERO);
    }
}