package com.yidianling.course.courseNew.sections;

import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.yidianling.common.tools.RxDeviceTool;
import com.yidianling.course.R;
import com.yidianling.course.courseNew.CategoryItemView;
import com.yidianling.course.courseNew.LinkUrlRouterProtocol;
import com.yidianling.course.model.CatogoryBean;
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 java.util.List;

import cn.lankton.flowlayout.FlowLayout;

/**
 * Created by Jim on 2018/6/25 0025.
 */

public class CategorySection extends Section {

    private final int IDENTIFY_TYPE = CATEGORY_SECTION;

    public List<CatogoryBean> datas;

    public CategorySection(List<CatogoryBean> datas) {
        super(R.layout.item_category2);
        this.datas = datas;
    }

    @Override
    public void bindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CategoryViewHolder) {

        }
    }

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

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

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(itemResourceId, parent, false);
        return new CategoryViewHolder(view, datas);
    }

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

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

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

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

    static class CategoryViewHolder extends RecyclerView.ViewHolder {

        public CategoryViewHolder(View itemView, List<CatogoryBean> datas) {
            super(itemView);
            int width = RxDeviceTool.getScreenWidth(itemView.getContext());
            FlowLayout flowLayout = itemView.findViewById(R.id.flow_layout);
            int itemWidth = width / 4;
            for (int i = 0; i < datas.size(); i++) {
                CatogoryBean category = datas.get(i);
                CategoryItemView ciView = new CategoryItemView(itemView.getContext());
                ViewGroup.MarginLayoutParams params = new ViewGroup.MarginLayoutParams(itemWidth, WindowManager.LayoutParams.WRAP_CONTENT);
                ciView.setLayoutParams(params);
                ciView.setText(category.title);
                ciView.setImage(category.image);
                ciView.setOnClickListener(view -> {

                    UMEventUtils.umEvent("课程分类点击", "分类id", category.linkUrl);
                    UMEventUtils.umEvent("课程分类点击", "分类名称", category.title);

                    ActionCountUtils.Companion.count(BIConstants.CourseHomeEvent.POSITION_COURSE_HOME_CLASSIFICATION_CLICK, category.title);
                    if (!TextUtils.isEmpty(category.linkUrl)) {
                        LinkUrlRouterProtocol.jump(itemView.getContext(), category.linkUrl);
                    }
                });
                flowLayout.addView(ciView);
            }
        }
    }
}
