package com.yidianling.course.courseNew.sections;

import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.yidianling.course.R;
import com.yidianling.course.bean.CourseHomeBodyBean;
import com.yidianling.course.courseNew.LinkUrlRouterProtocol;
import com.yidianling.course.courseNew.viewholder.SingleTitleHeaderViewHolder;
import com.yidianling.course.link.CourseLinkUrl;
import com.yidianling.course.model.NormalCourseBean;
import com.yidianling.ydlcommon.adapter.section.Section;
import com.yidianling.ydlcommon.utils.UMEventUtils;
import com.yidianling.ydlcommon.utils.actionutil.ActionCountUtils;
import com.yidianling.ydlcommon.utils.actionutil.BIConstants;
import com.yidianling.ydlcommon.view.BaseViewHolder;

import java.util.List;

/**
 * Created by Jim on 2018/6/27 0027.
 */

public class TopicsCoursesSection extends Section {

    private final int IDENTIFY_TYPE = TOPICS_COURSES_SECTION;
    private Context mContext;
    private NormalCourseBean data;

    public TopicsCoursesSection(Context mContext, NormalCourseBean data) {
        super(R.layout.item_classified_title, 0, R.layout.item_topics_courses);
        this.mContext = mContext;
        this.data = data;
    }

    @Override
    public void bindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof SingleTitleHeaderViewHolder) {
            if (data.head != null) {
                if (!TextUtils.isEmpty(data.head.title)) {
                    ((SingleTitleHeaderViewHolder) holder).titleView.setText(data.head.title);
                }
                if (!TextUtils.isEmpty(data.head.subTitle)) {
                    ((SingleTitleHeaderViewHolder) holder).subTitleView.setVisibility(View.VISIBLE);
                    ((SingleTitleHeaderViewHolder) holder).subTitleView.setText(data.head.subTitle);
                    ((SingleTitleHeaderViewHolder) holder).subTitleView.setOnClickListener(v -> {
                        if (null != mContext) {
                            CourseLinkUrl.Companion.jump(mContext, data.head.subLinkUrl);
                        }

                        ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_THEMATIC_COURSES_MORE_CLICK);
                    });
                } else {
                    ((SingleTitleHeaderViewHolder) holder).subTitleView.setVisibility(View.GONE);
                }
            }

        } else if (holder instanceof TopicsCoursesVH) {
            ((TopicsCoursesVH) holder).notifyData(data.body);
        }
    }

    @Override
    public int getTotalDataNum() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        if (isHeadType(position)) {
            return combineType(IDENTIFY_TYPE, TYPE_HEADER);
        }
        return combineType(IDENTIFY_TYPE, TYPE_BODY);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = null;
        if (viewType == combineType(IDENTIFY_TYPE, TYPE_HEADER)) {
            view = LayoutInflater.from(parent.getContext()).inflate(headerResourceId, parent, false);
            return new SingleTitleHeaderViewHolder(view);
        }
        view = LayoutInflater.from(parent.getContext()).inflate(itemResourceId, parent, false);
        return new TopicsCoursesVH(view, data.body);
    }

    @Override
    public boolean isBelongTo(int viewType) {
        return String.valueOf(viewType).startsWith(String.valueOf(IDENTIFY_TYPE));
    }

    @Override
    public boolean isHeadType(int position) {
        return data.head != null && position == 0;
    }

    @Override
    public boolean isFooterType(int position) {
        return false;
    }

    @Override
    public int getSectionId() {
        return 0;
    }

    static class TopicsCoursesVH extends RecyclerView.ViewHolder {
        private RecyclerView mRecyclerView;
        private Adapter mAdapter;

        public TopicsCoursesVH(View itemView, List<CourseHomeBodyBean> data) {
            super(itemView);
            mRecyclerView = itemView.findViewById(R.id.rcv_topics_courses);
            LinearLayoutManager linearLayoutManager = new LinearLayoutManager(itemView.getContext());
            linearLayoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            mRecyclerView.setLayoutManager(linearLayoutManager);
            mAdapter = new Adapter(data, itemView.getContext());
            mRecyclerView.setAdapter(mAdapter);
        }

        public void notifyData(List<CourseHomeBodyBean> data) {
            mAdapter.data = data;
            mAdapter.notifyDataSetChanged();
        }

    }

    static class Adapter extends RecyclerView.Adapter<BaseViewHolder> {

        private List<CourseHomeBodyBean> data;
        private Context mContext;

        public Adapter(List<CourseHomeBodyBean> data, Context context) {
            this.data = data;
            mContext = context;
        }

        public void setData(List<CourseHomeBodyBean> data) {
            this.data = data;
        }

        @Override
        public BaseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_imag, parent, false);
            return new BaseViewHolder(view);
        }

        @Override
        public void onBindViewHolder(BaseViewHolder holder, int position) {
            holder.setImageWithUrl(R.id.img, data.get(position).image);
            holder.setOnClickListener(R.id.img, view -> {

                LinkUrlRouterProtocol.jump(mContext, data.get(position).linkUrl);

                ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_THEMATIC_COURSES_XXX_CLICK + data.get(position).id + "_click");
            });
        }

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