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.MainProjectListContract;
import kr.co.hanwha.hifive.adapter.model.UnitListViewHolderData;
import kr.co.hanwha.hifive.adapter.viewholder.MainEmptyFamousSayingListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainProjectCampaignHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainProjectMarginListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainProjectHeaderViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MainProjectTaskListViewHolder;
import kr.co.hanwha.hifive.adapter.viewholder.MarginViewHolder;
import kr.co.hanwha.hifive.data.model.MainProjectRoot;
import kr.co.hanwha.hifive.data.model.ProjectCampaignsInfo;
import kr.co.hanwha.hifive.data.model.ProjectCounterMeasuresInfo;
import kr.co.hanwha.hifive.data.model.ProjectMembersInfo;
import kr.co.hanwha.hifive.data.model.SpinnerData;
import kr.co.hanwha.hifive.enums.ViewHolderUnitType;
import kr.co.hanwha.hifive.util.HFCommonUtil;

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

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

    private final int ZERO = 0;

    private int mProject_id = 0;
    private ArrayList<SpinnerData> mListProjects;
    private ArrayList<UnitListViewHolderData> mListData;
    private MainProjectListContract.OnMainProjectListListener mOnMainProjectListListener;
    private int mProjectHeaderHeight = 0;

    public MainProjectListAdapter(@NonNull Context context,
                                  @NonNull MainProjectListContract.OnMainProjectListListener onMainProjectListListener) {
        mContext = context;
        mOnMainProjectListListener = onMainProjectListListener;
    }

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

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

    /**************************************************
     * Model
     **************************************************/
    @Override
    public void setProjectId(int projectId) {
        mProject_id = projectId;
    }

    @Override
    public void addItems(ArrayList<SpinnerData> listProjects) {
        if (mListProjects == null) mListProjects = new ArrayList<>();
        else mListProjects.clear();

        if (listProjects != null)
            mListProjects.addAll(listProjects);
    }

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

        int margin = HFCommonUtil.getPixelFromDimension(mContext, R.dimen.dimen_dp16);

        // Project Info
        mListData.add(new UnitListViewHolderData(ViewHolderUnitType.HEADER_PROJECT_INFO,
                data.getProjectInfo(),
                data.getListProjectMembersInfo(),
                data.getListProjectCountermeasuresInfo()));

        // Campaign & TASK
        if (data.getListProjectCampaignsInfo() != null && data.getListProjectCampaignsInfo().size() > 0) {
            for (int i = 0; i < data.getListProjectCampaignsInfo().size(); i++) {
                // Campaign contents
                ProjectCampaignsInfo campaign = data.getListProjectCampaignsInfo().get(i);
                campaign.setNumber(i + 1);
                mListData.add(new UnitListViewHolderData(ViewHolderUnitType.HEADER_CAMPAIGN, campaign, (campaign.getListSubTask() != null ? campaign.getListSubTask().size() : 0)));

                // TASK
                if (campaign.getListSubTask() != null) {
                    for (int j = 0; j < campaign.getListSubTask().size(); j++) {
                        mListData.add(new UnitListViewHolderData(ViewHolderUnitType.CONTENTS_TASK, campaign.getListSubTask().get(j), j, campaign.getListSubTask().size()));
                    }
                }
            }

            mListData.add(new UnitListViewHolderData(ViewHolderUnitType.MARGIN, null, margin));
        } else {
            mListData.add(new UnitListViewHolderData(ViewHolderUnitType.FAMOUS_SAYING_MARGIN, null, 0));
        }
    }

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

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ViewHolderUnitType.HEADER_PROJECT_INFO.getCode()) {
            return new MainProjectHeaderViewHolder(parent, mOnMainProjectListListener);
        } else if (viewType == ViewHolderUnitType.HEADER_CAMPAIGN.getCode()) {
            return new MainProjectCampaignHeaderViewHolder(parent);
        } else if (viewType == ViewHolderUnitType.CONTENTS_TASK.getCode()) {
            return new MainProjectTaskListViewHolder(parent, mOnMainProjectListListener);
        } else if (viewType == ViewHolderUnitType.MARGIN.getCode()) {
            return new MainProjectMarginListViewHolder(parent);
        } else if (viewType == ViewHolderUnitType.FAMOUS_SAYING_MARGIN.getCode()) {
//            return new MainEmptyFamousSayingListViewHolder(parent);
            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(RecyclerView.ViewHolder holder, int position) {
        UnitListViewHolderData data = mListData.get(position);

        if (getItemViewType(position) == ViewHolderUnitType.HEADER_PROJECT_INFO.getCode()) {
            MainProjectHeaderViewHolder view = (MainProjectHeaderViewHolder) holder;

            view.setListProjects(mListProjects);
            view.setProject_id(mProject_id);

            if (data.getData2() != null) {
                view.setListProjectMembersInfo((ArrayList<ProjectMembersInfo>) data.getData2());
            }
            if (data.getData3() != null) {
                view.setListProjectCounterMeasures((ArrayList<ProjectCounterMeasuresInfo>) data.getData3());
            }
            view.onBind(data.getData(), position);
            mProjectHeaderHeight = view.getHeight();
        } else if (getItemViewType(position) == ViewHolderUnitType.HEADER_CAMPAIGN.getCode()) {
            MainProjectCampaignHeaderViewHolder view = (MainProjectCampaignHeaderViewHolder) holder;
            view.onBind(data.getData(), data.getIntValue1());
        } else if (getItemViewType(position) == ViewHolderUnitType.CONTENTS_TASK.getCode()) {
            MainProjectTaskListViewHolder view = (MainProjectTaskListViewHolder) holder;
            view.setTotalCount(data.getIntValue2());
            view.onBind(data.getData(), data.getIntValue1());
        } else if (getItemViewType(position) == ViewHolderUnitType.MARGIN.getCode()) {
            MainProjectMarginListViewHolder view = (MainProjectMarginListViewHolder) holder;
            view.setColorRes(R.color.hf_grey21);
            view.onBind(null, data.getIntValue1());
        } else if (getItemViewType(position) == ViewHolderUnitType.FAMOUS_SAYING_MARGIN.getCode()) {
            int height = HFCommonUtil.getScreenHeight(mContext)
                    - HFCommonUtil.getStatusBarHeight(mContext)
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_top_toolbar_height)
                    - mProjectHeaderHeight
                    - HFCommonUtil.getPixelFromDimension(mContext, R.dimen.main_bottom_tabbar_height);

            MarginViewHolder view = (MarginViewHolder) holder;
//            view.setColorRes(android.R.color.transparent);
            view.setColorRes(R.color.hf_grey21);
            view.onBind(mContext.getString(R.string.main_project_task_empty), height);
//            MainEmptyFamousSayingListViewHolder view = (MainEmptyFamousSayingListViewHolder) holder;
//            view.onBind(null, height);
        }
    }

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