package kr.co.hanwha.hifive.admin.admin.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
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.viewholder.MainEmptyListViewHolder;
import kr.co.hanwha.hifive.admin.admin.adapter.viewholder.AdminMemberDetailHeaderViewHolder;
import kr.co.hanwha.hifive.admin.admin.adapter.viewholder.AdminMemberDetailInfoViewHolder;
import kr.co.hanwha.hifive.admin.network.model.AdminMemberDetailHrInfo;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

public class AdminMemberDetailAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private Context mContext;

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

    private final int TYPE_EMPTY = 0;
    private final int TYPE_HEADER = 1;
    private final int TYPE_INFO = 2;

    private ArrayList<AdminMemberDetailHrInfo> mListData;

    private OnAdminMemberDetailListener mOnAdminMemberDetailListener;

    public AdminMemberDetailAdapter(@NonNull Context context, @Nullable OnAdminMemberDetailListener onAdminMemberDetailListener) {
        mContext = context;
        mOnAdminMemberDetailListener = onAdminMemberDetailListener;
    }

    /**************************************************
     * View
     **************************************************/
    @Override
    public int getItemViewType(int position) {
        if (isEmpty())
            return TYPE_EMPTY;
        else if (position == 0) return TYPE_HEADER;
        else return TYPE_INFO;
    }

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

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

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

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

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_HEADER)
            return new AdminMemberDetailHeaderViewHolder(parent, mOnAdminMemberDetailListener);
        else if (viewType == TYPE_INFO)
            return new AdminMemberDetailInfoViewHolder(parent);
        else if (viewType == TYPE_EMPTY)
            return new MainEmptyListViewHolder(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_HEADER) {
            int index = position;
            AdminMemberDetailHrInfo data = mListData.get(index);
            AdminMemberDetailHeaderViewHolder view = (AdminMemberDetailHeaderViewHolder) holder;
            view.onBind(data, index);
        } else if (getItemViewType(position) == TYPE_INFO) {
            int index = position - COUNT_HEADER;
            AdminMemberDetailHrInfo data = mListData.get(index);
            AdminMemberDetailInfoViewHolder view = (AdminMemberDetailInfoViewHolder) 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() + COUNT_HEADER : ZERO));
    }

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

    /**************************************************
     * Interface
     **************************************************/
    public interface OnAdminMemberDetailListener {
        void onResetSimplePasswordClicked();

        void onResetPasswordClicked();

        void onTaskListClicked();
    }

}
