package com.bokecc.livemodule.live.courseware.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bokecc.common.utils.DensityUtil;
import com.bokecc.dwlivedemo.R;
import com.bokecc.livemodule.live.courseware.bean.CoursewareBean;
import com.bokecc.livemodule.view.DownloadProgressButton;

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

/**
 * 直播课件下载适配器
 */
public class CoursewareAdapter extends RecyclerView.Adapter<CoursewareAdapter.CustomViewHolder> {

    private Context mContext;
    private List<CoursewareBean> list;
    private LayoutInflater mInflater;
    private OnDownClickListener downClickListener;

    public interface OnDownClickListener {
        void onDownClick(CoursewareBean bean);
    }

    public void setOnDownClickListener(OnDownClickListener listener) {
        this.downClickListener = listener;
    }

    public CoursewareAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    public List<CoursewareBean> getList() {
        if (list==null){
            list = new ArrayList<>();
        }
        return list;
    }

    public void setList(List<CoursewareBean> list) {
        this.list = list;
        notifyDataSetChanged();
    }


    @NonNull
    @Override
    public CustomViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new CustomViewHolder(mInflater.inflate(R.layout.item_live_courseware, viewGroup, false));
    }

    @Override
    public void onBindViewHolder(final CustomViewHolder holder, @SuppressLint("RecyclerView") final int position) {
        final CoursewareBean bean = list.get(position);
        if (bean!=null){
            holder.downloadBtn.setMaxProgress(100);
            holder.downloadBtn.setMinProgress(0);
            holder.downloadBtn.setButtonRadius(DensityUtil.dp2px(mContext,20));
            holder.tvCoursewareSpeed.setVisibility(View.GONE);
            holder.tvCoursewareProgress.setVisibility(View.GONE);

            setIcon(holder,bean.getType());

            holder.tvCoursewareName.setText(bean.getName());
            String size = formatBytes(bean.getSize());
            holder.tvCoursewareSize.setText(size);
            if (bean.getStatus()==CoursewareBean.STATUS_NORMAL){
                holder.downloadBtn.setCurrentText("下载");
                holder.downloadBtn.setProgress(0);
                holder.downloadBtn.setState(DownloadProgressButton.STATE_NORMAL);
                holder.downloadBtn.postInvalidate();
            }else if (bean.getStatus()==CoursewareBean.STATUS_FETCHING){
                holder.downloadBtn.setState(DownloadProgressButton.STATE_NORMAL);
                holder.downloadBtn.setCurrentText("取消下载");

                holder.tvCoursewareSpeed.setVisibility(View.VISIBLE);
                holder.tvCoursewareProgress.setVisibility(View.VISIBLE);
                holder.tvCoursewareSpeed.setText(bean.getSpeed()+"kb/s");
                holder.tvCoursewareProgress.setText(bean.getProgress()+"%");
                holder.downloadBtn.setProgress(0);

                holder.downloadBtn.postInvalidate();
            }else {
                holder.downloadBtn.setState(DownloadProgressButton.STATE_FINISH);
                holder.downloadBtn.setCurrentText("打开");
            }
            holder.downloadBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    handleDownload(bean);
                }
            });
        }

    }
    public String formatBytes(long bytes) {
        final long KB = 1024;
        final long MB = KB * 1024;
        final long GB = MB * 1024;

        if (bytes < KB) {
            return bytes + " B";
        } else if (bytes < MB) {
            return String.format("%.2f KB", (float) bytes / KB);
        } else if (bytes < GB) {
            return String.format("%.2f MB", (float) bytes / MB);
        } else {
            return String.format("%.2f GB", (float) bytes / GB);
        }
    }
    private void setIcon(CustomViewHolder holder, int type) {
        if (type==CoursewareBean.FILE_TYPE_PDF){
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_pdf);
        }else if (type==CoursewareBean.FILE_TYPE_DOC){
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_word);
        }else if (type==CoursewareBean.FILE_TYPE_IMAGE){
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_image);
        }else if (type==CoursewareBean.FILE_TYPE_PPT){
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_ppt);
        }else if (type==CoursewareBean.FILE_TYPE_GIF){
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_image);
        }else {
            holder.ivCoursewareIcon.setImageResource(R.drawable.icon_courseware_default);
        }
    }

    private void handleDownload(CoursewareBean bean) {
        if (downClickListener!=null){
            downClickListener.onDownClick(bean);
        }
    }


    @Override
    public int getItemCount() {
        return list==null?0:list.size();
    }

    final class CustomViewHolder extends RecyclerView.ViewHolder {

        private final DownloadProgressButton downloadBtn;
        private final TextView tvCoursewareSpeed;
        private final TextView tvCoursewareProgress;
        public ImageView ivCoursewareIcon;
        public TextView tvCoursewareName;
        public TextView tvCoursewareSize;

        CustomViewHolder(View itemView) {
            super(itemView);

            ivCoursewareIcon = itemView.findViewById(R.id.iv_courseware_icon);
            tvCoursewareName = itemView.findViewById(R.id.tv_courseware_name);
            tvCoursewareSize = itemView.findViewById(R.id.tv_courseware_size);
            downloadBtn = itemView.findViewById(R.id.download_btn);
            tvCoursewareSpeed = itemView.findViewById(R.id.tv_courseware_speed);
            tvCoursewareProgress = itemView.findViewById(R.id.tv_courseware_progress);

        }
    }
}