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.AlarmListContract;
import kr.co.hanwha.hifive.adapter.viewholder.AlarmListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.FooterLoadingViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyListViewHolder;
import kr.co.hanwha.hifive.data.model.AlarmData;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * 알림 목록 어댑터
 *
 * @see kr.co.hanwha.hifive.screen.alarm.AlarmActivity
 * @see kr.co.hanwha.hifive.screen.alarm.AlarmPresenter
 * Created by sanghoonkwon on 2018. 1. 22..
 */

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

    private final int ZERO = 0;
    private final int COUNT_EMPTY = 1;
    private final int COUNT_FOOTER_LOADING = 1;

    private final int TYPE_EMPTY = 0;
    private final int TYPE_ALARM = 1;
    private final int TYPE_FOOTER_LOADING = 2;

    private AlarmListContract.OnAlarmListListener mOnAlarmListListener;
    private ArrayList<AlarmData> mListData;
    private boolean isVisibleFooter = false;

    public AlarmListAdapter(@NonNull Context context,
                            @NonNull AlarmListContract.OnAlarmListListener onAlarmListListener) {
        mContext = context;
        mOnAlarmListListener = onAlarmListListener;
    }

    /**************************************************
     * View
     **************************************************/
    @Override
    public int getItemViewType(int position) {
        if (isEmpty())
            return TYPE_EMPTY;
        else if (isFooter(position))
            return TYPE_FOOTER_LOADING;
        else return TYPE_ALARM;
    }

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

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

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

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

    @Override
    public void replaceItem(int index, AlarmData data) {
        if (mListData != null && mListData.size() > index)
            mListData.set(index, data);
    }

    @Override
    public void setVisibleFooter(boolean isFooter) {
        isVisibleFooter = isFooter;
    }

    /**************************************************
     * Adapter
     **************************************************/

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_ALARM)
            return new AlarmListViewHolder(parent, mOnAlarmListListener);
        else if (viewType == TYPE_EMPTY)
            return new MainEmptyListViewHolder(parent);
        else if (viewType == TYPE_FOOTER_LOADING)
            return new FooterLoadingViewHolder(parent);

        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_ALARM) {
            int index = position;
            AlarmData data = mListData.get(index);
            AlarmListViewHolder view = (AlarmListViewHolder) holder;
            view.onBind(data, index);
        } else if (getItemViewType(position) == TYPE_EMPTY) {
            int height = HFCommonUtil.getScreenHeight(mContext)
                    - HFCommonUtil.getStatusBarHeight(mContext)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_top_toolbar_height);
            MainEmptyListViewHolder view = (MainEmptyListViewHolder) holder;
            view.onBind(mContext.getString(R.string.alarm_empty_data), height);
        }
    }

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

    private boolean isEmpty() {
        return (mListData == null || mListData.size() == 0);
    }

    private boolean isFooter(int position) {
        return (isVisibleFooter && isEmpty() == false && mListData.size() == position);
    }
}
