package com.yidianling.course.courseNew.sections;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

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.coursePlay.moudle.Course;
import com.yidianling.course.model.QulityCourseBean;
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 QulityCourseSection extends Section {

    private final int IDENTIFY_TYPE = QULITY_COURSE_SECTION;
    private QulityCourseBean data;
    private int totalNum = 0;

    private static final int FREE = 0;
    private static final int PAY = 1;
    public static int type = PAY;

    public void setType(int type) {
        this.type = type;
        initTotlaNum();
        adapter.notifyDataSetChanged();
    }


    private RecyclerView.Adapter adapter;
    private Context context;

    public QulityCourseSection(Context context, QulityCourseBean data, RecyclerView.Adapter adapter) {
        super(R.layout.item_head_quality_course, R.layout.item_more, R.layout.item_course_list);
        this.context = context;
        this.adapter = adapter;
        this.data = data;
        type = PAY;
        initTotlaNum();
    }

    private void 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(v -> {

                LinkUrlRouterProtocol.jump(context, data.footer.linkUrl + "?childId=" + (type + 1));

                ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_EXCELLENT_COURSES_MORE_CLICK);
            });
        } else if (holder instanceof QulityCourseHeaderVH) {
            ((QulityCourseHeaderVH) holder).tv_title.setText(data.head.title);
            ((QulityCourseHeaderVH) holder).tv_tab1.setText(data.head.tab1Name);
            ((QulityCourseHeaderVH) holder).tv_tab2.setText(data.head.tab2Name);
            if (type == PAY) {
                ((QulityCourseHeaderVH) holder).tv_tab2.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.green_34cd65));
                ((QulityCourseHeaderVH) holder).iv_tab2.setVisibility(View.VISIBLE);
                ((QulityCourseHeaderVH) holder).tv_tab1.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.grey_909599));
                ((QulityCourseHeaderVH) holder).iv_tab1.setVisibility(View.INVISIBLE);
            } else {
                ((QulityCourseHeaderVH) holder).tv_tab1.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.green_34cd65));
                ((QulityCourseHeaderVH) holder).iv_tab1.setVisibility(View.VISIBLE);
                ((QulityCourseHeaderVH) holder).tv_tab2.setTextColor(ContextCompat.getColor(holder.itemView.getContext(), R.color.grey_909599));
                ((QulityCourseHeaderVH) holder).iv_tab2.setVisibility(View.INVISIBLE);
            }
        } else if (holder instanceof ClassifiedCourseSection.CourseItemViewHolder) {
            ClassifiedCourseSection.CourseItemViewHolder realHolder = (ClassifiedCourseSection.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(type).get(position - 1);
            Course courseBean = switchData(course);
            realHolder.courseItemNewView.setData(courseBean);

            holder.itemView.setOnClickListener(view -> {

                LinkUrlRouterProtocol.jump(context, courseBean.linkUrl);

                if (type == PAY) {
                    ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_EXCELLENT_COURSES_PAY_XXX_CLICK + courseBean.id + "_click");
                } else {
                    ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_EXCELLENT_COURSES_FREE_XXX_CLICK + courseBean.id + "_click");
                }
            });
        }
    }


    /**
     * 这里是个坑
     *
     * @param bean
     * @return
     */
    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.pic = bean.pic;
        course.image = bean.image;
        course.hostInfo = bean.hostInfo;
        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.isBuy = bean.isBuy;
        course.demoFile = bean.demoFile;
        course.promotionApplyFee = bean.promotionApplyFee;
        course.courseType = bean.courseType;
        course.isPromotion = bean.isPromotion;
        course.linkUrl = bean.linkUrl;
        return course;
    }

    @Override
    public int getTotalDataNum() {
        totalNum = 0;
        if (data.head != null) {
            totalNum++;
        }
        if (data.footer != null) {
            totalNum++;
        }
        if (data.body != null) {
            totalNum += data.body.get(type).size();
        }
        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) {
        if (viewType == combineType(IDENTIFY_TYPE, TYPE_HEADER)) {
            View view = LayoutInflater.from(parent.getContext()).inflate(headerResourceId, parent, false);
            return new QulityCourseHeaderVH(this, view);
        } else if (viewType == combineType(IDENTIFY_TYPE, TYPE_FOOTER)) {
            View view = LayoutInflater.from(parent.getContext()).inflate(footerResourceId, parent, false);
            return new FooterMoreViewHolder(view);
        } else {
            View view = LayoutInflater.from(parent.getContext()).inflate(itemResourceId, parent, false);
            return new ClassifiedCourseSection.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 QulityCourseHeaderVH extends RecyclerView.ViewHolder {

        public TextView tv_title, tv_tab1, tv_tab2;
        public ImageView iv_tab1, iv_tab2;

        public QulityCourseHeaderVH(QulityCourseSection section, View itemView) {
            super(itemView);
            tv_title = itemView.findViewById(R.id.tv_title);
            tv_tab1 = itemView.findViewById(R.id.tv_tab1);
            iv_tab1 = itemView.findViewById(R.id.iv_tab1);
            tv_tab2 = itemView.findViewById(R.id.tv_tab2);
            iv_tab2 = itemView.findViewById(R.id.iv_tab2);
            itemView.findViewById(R.id.ll_tab1).setOnClickListener(v -> {

                tv_tab1.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.green_34cd65));
                iv_tab1.setVisibility(View.VISIBLE);
                tv_tab2.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.grey_909599));
                iv_tab2.setVisibility(View.INVISIBLE);
                section.setType(FREE);
            });
            itemView.findViewById(R.id.ll_tab2).setOnClickListener(v -> {

                tv_tab2.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.green_34cd65));
                iv_tab2.setVisibility(View.VISIBLE);
                tv_tab1.setTextColor(ContextCompat.getColor(itemView.getContext(), R.color.grey_909599));
                iv_tab1.setVisibility(View.INVISIBLE);
                section.setType(PAY);
            });
        }
    }

}
