package com.zj365.mime.adapter

import android.graphics.Rect
import android.os.Bundle
import android.text.TextUtils
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadSampleListener
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.util.FileDownloadUtils
import com.xty.base.adapter.BaseAdapter
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.util.OpenFilesUtils
import com.xty.network.model.EnterpriseInfoHomeBean
import com.zj365.mime.R
import java.io.File

class EnterpriseInformationAdapter : BaseAdapter<EnterpriseInfoHomeBean>(R.layout.item_enterprise_info) {

    override fun convert(holder: BaseViewHolder, item: EnterpriseInfoHomeBean) {

        holder.setText(R.id.tv_info_name,item.typeName)

        var  mAdapter = EnterpriseInformationChildAdapter(0)
        var recyclerView = holder.getView<RecyclerView>(R.id.recycler)

        recyclerView.layoutManager = LinearLayoutManager(context)
        recyclerView.adapter = mAdapter

        recyclerView.addItemDecoration(object : RecyclerView.ItemDecoration() {
            override fun getItemOffsets(outRect: Rect, itemPosition: Int, parent: RecyclerView) {
                super.getItemOffsets(outRect, itemPosition, parent)
                outRect.bottom = 12
            }
        })

        mAdapter.setOnItemClickListener { adapter, view, position ->
            LogUtils.e("EnterpriseInformationAdapter","position:${position}")

            var bean = adapter.data[position] as EnterpriseInfoHomeBean.EnterpriseInfoChildBean
             var filePath: String? = null

            when(bean.fileFormat){  //	格式1,word,2,pdf,3.文本，4.excel,5.音频。6.链接
                1->{

                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.doc"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

                        createDownloadTask(filePath,bean.fileUrl,bean.fileFormat)!!.start()
                    }

                }

                2->{

                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.pdf"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

                        LogUtils.e("EnterpriseInformationForTypeAct","filePath${filePath}")

                        createDownloadTask(filePath,bean.fileUrl,bean.fileFormat)!!.start()
                    }
                }
                3 ->{

                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.txt"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

                        LogUtils.e("EnterpriseInformationForTypeAct","filePath${filePath}")

                        createDownloadTask(filePath,bean.fileUrl,bean.fileFormat)!!.start()
                    }


                }
                4->{
                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.xlsx"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

                        LogUtils.e("EnterpriseInformationForTypeAct","filePath${filePath}")

                        createDownloadTask(filePath,bean.fileUrl,bean.fileFormat)!!.start()
                    }
                }
                5->{
                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.mp3"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

                        LogUtils.e("EnterpriseInformationForTypeAct","filePath${filePath}")

                        createDownloadTask(filePath,bean.fileUrl,bean.fileFormat)!!.start()
                    }

                }

                6->{
                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        var bundle = Bundle()
                        bundle.putString(
                            "web_site_url",
                            "${bean.fileUrl}"
                        )
                        bundle.putBoolean("isShowTitle",true)
                        bundle.putString("title",bean.title)
                        RouteManager.goAct(ARouterUrl.H5_ACT,bundle)
                    }
                }
            }
        }

        mAdapter.setNewInstance(item.list)
    }


    private fun createDownloadTask(filePath: String,url:String,fileFormat:Int): BaseDownloadTask? {
        var isDir = false
        return FileDownloader.getImpl().create(url)
            .setPath(filePath, false)
            .setCallbackProgressTimes(300)
            .setMinIntervalUpdateSpeed(400)
            .setTag(this)
            .setListener(object : FileDownloadSampleListener() {
                override fun pending(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    super.pending(task, soFarBytes, totalBytes)
                    LogUtils.e("EnterpriseInformationForTypeAct","pending${soFarBytes}")

                }

                override fun progress(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    super.progress(task, soFarBytes, totalBytes)

                    LogUtils.e("EnterpriseInformationForTypeAct","progress${soFarBytes}")
                }

                override fun error(task: BaseDownloadTask, e: Throwable) {
                    super.error(task, e)
                }

                override fun connected(
                    task: BaseDownloadTask,
                    etag: String,
                    isContinue: Boolean,
                    soFarBytes: Int,
                    totalBytes: Int
                ) {
                    super.connected(task, etag, isContinue, soFarBytes, totalBytes)
                    LogUtils.e("EnterpriseInformationForTypeAct","connected${soFarBytes}")

                }

                override fun paused(task: BaseDownloadTask, soFarBytes: Int, totalBytes: Int) {
                    super.paused(task, soFarBytes, totalBytes)
                }

                override fun completed(task: BaseDownloadTask) {
                    super.completed(task)
                    OpenFilesUtils.openFile(context, File(filePath))
                }

                override fun warn(task: BaseDownloadTask) {
                    super.warn(task)
                }
            })
    }
}