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.adapter.contract.SettingMyHomeOkrListContract;
import kr.co.hanwha.hifive.adapter.model.OkrListViewHolderData;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyFamousSayingListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SettingMyHomeOkrKeyResultViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SettingMyHomeOkrObjectViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.SettingMyHomeOkrUnitViewHolder;
import kr.co.hanwha.hifive.data.model.CampaignTreeData;
import kr.co.hanwha.hifive.data.model.MemberCampaignsTree;
import kr.co.hanwha.hifive.data.model.ProjectCampaignsTree;
import kr.co.hanwha.hifive.enums.ViewHolderOkrType;
import kr.co.hanwha.hifive.enums.ViewHolderTaskType;
import kr.co.hanwha.hifive.screen.main.setting.myhome.contract.MyHomeContract;
import kr.co.hanwha.hifive.util.HFCommonUtil;

/**
 * Created by aaron on 2018. 9. 18.
 */

public class SettingMyhomeOkrListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements SettingMyHomeOkrListContract.View,
        SettingMyHomeOkrListContract.Model {
    private final int ZERO = 0;
    private final float FOOTER_HEIGHT = 35.5f;

    private Context mContext;
    private MyHomeContract.OnSettingMyHomeOkrListListener mOnSettingMyHomeOkrListListener;
    private ArrayList<OkrListViewHolderData> mListData = new ArrayList<>();

    private int mProjectId;
    private String mProjectName;

    public SettingMyhomeOkrListAdapter(Context context, MyHomeContract.OnSettingMyHomeOkrListListener onSettingMyHomeOkrListListener) {
        this.mContext = context;
        this.mOnSettingMyHomeOkrListListener = onSettingMyHomeOkrListListener;
    }

    @Override
    public void addItems(ArrayList<CampaignTreeData> okrListData) {
        if (mListData != null && mListData.size() > 0)
            mListData.clear();

        if (okrListData == null) return;

        if (okrListData != null && okrListData.size() > 0) {
            addProjectItem(okrListData);
            mListData.add(new OkrListViewHolderData(ViewHolderOkrType.FOOTER_MARGIN, "", -1));
        } else {
            mListData.add(new OkrListViewHolderData(ViewHolderOkrType.FAMOUS_SAYING_MARGIN));
        }
    }

    private void addProjectItem(ArrayList<CampaignTreeData> okrListData) {
        for (CampaignTreeData treeData : okrListData) {
            mProjectId = treeData.getProject_id();
            mProjectName = treeData.getProject_name();
            OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                    treeData.getProject_id(),
                    treeData.getProject_name(),
                    null,
                    ViewHolderOkrType.UNIT
            );
            mListData.add(viewHolderData);
            if (treeData.getListProjectCampaignsTree() != null && treeData.getListProjectCampaignsTree().size() > 0) {
                addProjectCampaignItem(treeData.getListProjectCampaignsTree());
            }
        }
    }

    private void addProjectCampaignItem(ArrayList<ProjectCampaignsTree> projectCampaignsListData) {
        for (ProjectCampaignsTree projectCampaignData : projectCampaignsListData) {
            OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                    projectCampaignData.getProject_campaign_id(),
                    null,
                    projectCampaignData.getProject_campaign_desc(),
                    ViewHolderOkrType.OBJECT
            );
            if (projectCampaignData.getListMemberCampaignsTree() != null && projectCampaignData.getListMemberCampaignsTree().size() > 0) {
                mListData.add(viewHolderData);
                addMemberCampaignItem(projectCampaignData.getListMemberCampaignsTree());
            } else {
                viewHolderData.setHasEmptyKr(true);
                mListData.add(viewHolderData);
            }
        }
    }

    private void addMemberCampaignItem(ArrayList<MemberCampaignsTree> memberCampaignsListData) {
        for (int i = 0; i < memberCampaignsListData.size(); i++) {
            MemberCampaignsTree memberCampaignData = memberCampaignsListData.get(i);
            OkrListViewHolderData viewHolderData = new OkrListViewHolderData(
                    memberCampaignData.getMember_campaign_id(),
                    null,
                    memberCampaignData.getMember_campaign_desc(),
                    ViewHolderOkrType.KEY_RESULT
            );
            if (i == memberCampaignsListData.size() - 1) {
                viewHolderData.setLast(true);
            }
            viewHolderData.setProjectId(mProjectId);
            viewHolderData.setProjectName(mProjectName);
            mListData.add(viewHolderData);
        }
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == ViewHolderOkrType.UNIT.getCode()) {
            return new SettingMyHomeOkrUnitViewHolder(parent);
        } else if (viewType == ViewHolderOkrType.OBJECT.getCode()) {
            return new SettingMyHomeOkrObjectViewHolder(parent);
        } else if (viewType == ViewHolderOkrType.KEY_RESULT.getCode()) {
            return new SettingMyHomeOkrKeyResultViewHolder(parent, mOnSettingMyHomeOkrListListener);
        } else if (viewType == ViewHolderTaskType.FAMOUS_SAYING_MARGIN.getCode()) {
            return new MainEmptyFamousSayingListViewHolder(parent);
        } else if (viewType == ViewHolderOkrType.FOOTER_MARGIN.getCode()) {
            return new MarginViewHolder(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(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SettingMyHomeOkrUnitViewHolder) {
            SettingMyHomeOkrUnitViewHolder view = (SettingMyHomeOkrUnitViewHolder) holder;
            view.onBind(mListData.get(position), position);
        } else if (holder instanceof SettingMyHomeOkrObjectViewHolder) {
            SettingMyHomeOkrObjectViewHolder view = (SettingMyHomeOkrObjectViewHolder) holder;
            view.onBind(mListData.get(position), position);
        } else if (holder instanceof SettingMyHomeOkrKeyResultViewHolder) {
            SettingMyHomeOkrKeyResultViewHolder view = (SettingMyHomeOkrKeyResultViewHolder) holder;
            view.onBind(mListData.get(position), position);
        } else if (holder instanceof MainEmptyFamousSayingListViewHolder) {
            MainEmptyFamousSayingListViewHolder view = (MainEmptyFamousSayingListViewHolder) holder;
            view.onBind(null, mListData.get(position).getIntValue());
        } else if (holder instanceof MarginViewHolder) {
            MarginViewHolder view = (MarginViewHolder) holder;
            view.onBind(null, (int) HFCommonUtil.getPixelFromDp(mContext, FOOTER_HEIGHT));
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        return (mListData != null && mListData.size() > 0) ? mListData.get(position).getType().getCode() : ZERO;
    }

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

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