package com.bokecc.livemodule.replay.summary;

import android.content.Context;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bokecc.livemodule.R;
import com.bokecc.livemodule.replay.summary.adapter.SummaryItemBean;
import com.bokecc.livemodule.replay.summary.adapter.SummaryListAdapter;
import com.bokecc.sdk.mobile.live.pojo.ReplaySummaryInfo;

import java.util.ArrayList;
import java.util.List;


public class ReplaySummaryComponent extends LinearLayout {

    private Context mContext;

    private SummaryListAdapter adapter;

    private OnSelectSummaryChange onSelectSummaryChange;
    private RecyclerView rvSummaryList;
    private TextView tvSummaryContent;


    public ReplaySummaryComponent(Context context) {
        super(context);
        mContext = context;
        initView();
    }

    public ReplaySummaryComponent(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        initView();
    }

    public void initView() {
        View rootView = View.inflate(mContext, R.layout.replay_summary_component, this);
        rvSummaryList = rootView.findViewById(R.id.rv_summary_list);
        tvSummaryContent = rootView.findViewById(R.id.tv_summary_content);
        adapter = new SummaryListAdapter(mContext);
        adapter.setOnItemClickListener(new SummaryListAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, SummaryItemBean summaryItemBean) {
                if (onSelectSummaryChange!=null){
                    onSelectSummaryChange.onSelectSummaryChange(summaryItemBean);
                }
            }
        });

        rvSummaryList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false));
        rvSummaryList.setAdapter(adapter);
    }

    public void setData(ReplaySummaryInfo summaryInfo){
        if (summaryInfo==null) {
            return;
        }
        if (summaryInfo.getSummary()!=null){
            tvSummaryContent.setText(summaryInfo.getSummary());
        }
        List<ReplaySummaryInfo.ChaptersDTO> replaySummaryList = summaryInfo.getChapters();
        if (replaySummaryList!=null){
            List<SummaryItemBean> summaryItemBeanList = new ArrayList<>();
            for (int i = 0; i <replaySummaryList.size(); i++) {
                ReplaySummaryInfo.ChaptersDTO replaySummary = replaySummaryList.get(i);
                SummaryItemBean summaryItemBean = new SummaryItemBean();
                summaryItemBean.setTitle(replaySummary.getHeadline());
                summaryItemBean.setTime(replaySummary.getStart());
                summaryItemBeanList.add(summaryItemBean);
            }

            adapter.setList(summaryItemBeanList);

        }
    }

    public void updateTime(long time) {
        if (adapter!=null){
            adapter.selectItemByTime(time);
        }
    }


    public void setOnSelectSummaryChange(OnSelectSummaryChange onSelectSummaryChange) {
        this.onSelectSummaryChange = onSelectSummaryChange;
    }

    public interface OnSelectSummaryChange{
        void onSelectSummaryChange(SummaryItemBean summaryItemBean);
    }

}