package com.yidianling.course.courseNew.sections;

import android.content.Context;
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.FooterMoreViewHolder;
import com.yidianling.course.courseNew.viewholder.SingleTitleHeaderViewHolder;
import com.yidianling.course.coursePlay.moudle.Course;
import com.yidianling.course.model.NormalCourseBean;
import com.yidianling.course.widget.CourseItemNewView;
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;

/**
 * Created by Jim on 2018/6/25 0025.
 * 分类课程模版
 */

public class ClassifiedCourseSection extends Section {

    private final int IDENTIFY_TYPE = CLASSIFY_COURSE_SECTION;
    private NormalCourseBean data;
    private int totalNum;

    private Context context;

    public ClassifiedCourseSection(Context context, NormalCourseBean bean) {
        super(R.layout.item_classified_title, R.layout.item_more, R.layout.item_course_list);
        this.context = context;
        this.data = bean;
        initTotlaNum();
    }

    @Override
    public void bindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof FooterMoreViewHolder) {
            ((FooterMoreViewHolder) holder).titleView.setText(data.footer.title);
            ((FooterMoreViewHolder) holder).linearLayout.setOnClickListener(view -> {
                LinkUrlRouterProtocol.jump(context, data.footer.linkUrl);

                if (!TextUtils.isEmpty(data.head.title)) {
                    if (data.head.title.contains("线下")) {
                        ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_OFFLINE_CERTIFICATION_TRAINING_MORE_CLICK);
                    } else {
                        ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_CONSULTANT_COURSE_MORE_CLICK);
                    }
                }
            });
        } else if (holder instanceof SingleTitleHeaderViewHolder) {
            ((SingleTitleHeaderViewHolder) holder).titleView.setText(data.head.title);
        } else if (holder instanceof CourseItemViewHolder) {
            CourseItemViewHolder realHolder = (CourseItemViewHolder) holder;
//            if ((data.head != null && position == 1) || (data.head == null && position == 0)) {
//                realHolder.hideDividerLine();
//            } else {
//                realHolder.tv_line_bottom.setVisibility(View.VISIBLE);
//            }
            CourseHomeBodyBean course = data.body.get(position - 1);

            Course courseBean = switchData(course);
            realHolder.courseItemNewView.setData(courseBean);

            holder.itemView.setOnClickListener(view -> {

                LinkUrlRouterProtocol.jump(context, courseBean.linkUrl);

                if (!TextUtils.isEmpty(data.head.title)) {
                    if (data.head.title.contains("线下")) {
                        ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_OFFLINE_CERTIFICATION_TRAINING_XXX_CLICK + courseBean.id + "_click");
                    }
                }
            });
        }
    }

    private Course switchData(CourseHomeBodyBean bean) {
        Course course = new Course();
        course.id = bean.id;
        course.title = bean.title;
        course.hostListPic = bean.hostListPic;
        course.courseHostInfo = bean.courseHostInfo;
        course.hostInfo = bean.hostInfo;
        course.pic = bean.pic;
        course.image = bean.image;
        course.subCourseNum = bean.subCourseNum;
        course.doctorName = bean.doctorName;
        course.joinNum = bean.joinNum;
        course.isInvite = bean.isInvite;
        course.applyFee = bean.applyFee;
        course.recommend = bean.recommend;
        course.newCourse = bean.newCourse;
        course.demoType = bean.demoType;
        course.originalApplyFee = bean.originalApplyFee;
        course.courseType = bean.courseType;
        course.isBuy = bean.isBuy;
        course.demoFile = bean.demoFile;
        course.promotionApplyFee = bean.promotionApplyFee;
        course.isPromotion = bean.isPromotion;
        course.linkUrl = bean.linkUrl;
        return course;
    }


    private void initTotlaNum() {
        totalNum = 0;
        if (data.head != null) {
            totalNum++;
        }
        if (data.footer != null) {
            totalNum++;
        }
        if (data.body != null) {
            totalNum += data.body.size();
        }
    }

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

    @Override
    public int getItemViewType(int position) {
        if (isHeadType(position)) {
            return combineType(IDENTIFY_TYPE, TYPE_HEADER);
        } else if (isFooterType(position)) {
            return combineType(IDENTIFY_TYPE, TYPE_FOOTER);
        } else {
            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(R.layout.item_classified_title, parent, false);
            return new SingleTitleHeaderViewHolder(view);
        } else if (viewType == combineType(IDENTIFY_TYPE, TYPE_FOOTER)) {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_more, parent, false);
            return new FooterMoreViewHolder(view);
        } else {
            view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_course_list, parent, false);
            return new CourseItemViewHolder(view);
        }
    }

    @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 data.footer != null && position == totalNum - 1;
    }

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


    static class CourseItemViewHolder extends RecyclerView.ViewHolder {
        public CourseItemNewView courseItemNewView;
        public View tv_line_bottom;

        public CourseItemViewHolder(View itemView) {
            super(itemView);
            courseItemNewView = itemView.findViewById(R.id.view_content);
            tv_line_bottom = itemView.findViewById(R.id.tv_line_bottom);
        }

        public void hideDividerLine() {
            tv_line_bottom.setVisibility(View.INVISIBLE);
        }
    }

}
