package com.cr.adapter

import android.content.Context
import android.graphics.Color
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.ProgressBar
import android.widget.TextView
import com.cr.common.FileManager
import com.cr.cruav.R
import com.cr.data.CrUtil
import com.cr.dialog.DialogNormal
import com.cr.models.DownloadDataModel
import com.cr.network.NetManager
import com.cr.network.TCPDataTask

/**
 * 操作系统：MAC系统
 * 创建者：王成
 * 创建日期：2023/4/11 16:15
 * 描述：数据下载适配器
 */
class DownloadDataAdapter @JvmOverloads constructor(
    context: Context,
    dataList: MutableList<DownloadDataModel>
) : CrAdapter<DownloadDataModel>(context, dataList),View.OnClickListener {
    // todo: 2023/4/11 多线程
    private val handler:Handler = object:Handler(Looper.getMainLooper()){
        override fun handleMessage(msg: Message) {
            super.handleMessage(msg)
        }
    }
    companion object {
        class ViewHolder {
            var lblTitle: TextView? = null
            var lblIsDownload: TextView? = null
            var btnDownload:ImageView?=null
            var progressBar:ProgressBar?=null
            var model:DownloadDataModel? = null
        }
    }

    /**
     * 重写获取视图
     * @param position Int
     * @param convertView View?
     * @param viewGroup ViewGroup?
     * @return View?
     */
    override fun getView(position: Int, convertView: View?, viewGroup: ViewGroup?): View? {
        var viewHolder:ViewHolder
        var view = convertView
        if(convertView == null){
            viewHolder = ViewHolder()
            view = inflater?.inflate(R.layout.item_data_download,null)
            viewHolder.lblTitle = view?.findViewById(R.id.item_down_title)
            viewHolder.lblIsDownload = view?.findViewById(R.id.item_down_state)
            viewHolder.btnDownload = view?.findViewById(R.id.item_down_down)
            viewHolder.progressBar = view?.findViewById(R.id.item_down_bar)
            view?.tag = viewHolder
        }else{
            viewHolder = view?.tag as ViewHolder
        }
        // todo: 2023/4/11 初始化数据
        var model:DownloadDataModel = dataList!![position]
        // todo: 2023/4/12 判断文件是否存在
        var fullName = CrUtil.CONFIG_PATH + model.fileName
        model.isDownload = FileManager.isExists(fullName)
        model.let {
            viewHolder.lblTitle?.text = it.title
            viewHolder.lblIsDownload?.text = if (model.isDownload) "已下载" else "未下载"
            viewHolder.lblIsDownload?.setTextColor(
                if (model.isDownload) Color.argb(
                    255,
                    255,
                    255,
                    255
                ) else Color.argb(255, 255, 0, 0)
            )
            // todo: 2023/4/11 设置最大值
            viewHolder.progressBar?.max = 100
            viewHolder.progressBar?.progress = 0
            viewHolder.model = model
            viewHolder.btnDownload?.tag = viewHolder
            viewHolder.btnDownload?.setOnClickListener(this)
        }
        return view!!
    }

    /**
     * 视图点击事件
     * @param view View
     */
    override fun onClick(view: View?) {
        when(view?.id){
            R.id.item_down_down->{
                var holder = view.tag as ViewHolder
                var url = "${NetManager.getServerUrl()}/resource/download/${holder.model?.fileName}"
                var fullName = CrUtil.CONFIG_PATH + holder.model?.fileName
                TCPDataTask.getInstance().sendDownloadFile(context!!,fullName,url,object:TCPDataTask.OnDownloadListener{
                    // todo: 2023/4/12 开始下载
                    override fun onStart() {
                        holder.progressBar?.progress = 0
                    }

                    // todo: 2023/4/12 下载错误
                    override fun onFailed(message: String) {
                        DialogNormal(context!!,"警告",message).show()
                    }

                    // todo: 2023/4/12 下载进度
                    override fun onProgress(progress: Int, total: Int) {
                        holder.progressBar?.max = total
                        holder.progressBar?.progress = progress
                    }

                    // todo: 2023/4/12 下载完成
                    override fun onComplete() {
                        holder.model?.isDownload = FileManager.isExists(fullName)
                        holder.lblIsDownload?.text = if (holder.model!!.isDownload) "已下载" else "未下载"
                        holder.lblIsDownload?.setTextColor(
                            if (holder.model!!.isDownload) Color.argb(
                                255,
                                255,
                                255,
                                255
                            ) else Color.argb(255, 255, 0, 0)
                        )
                    }

                })
            }
        }
    }
}