package com.zj365.mime.act

import android.os.Bundle
import android.text.TextUtils
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.liulishuo.filedownloader.BaseDownloadTask
import com.liulishuo.filedownloader.FileDownloadSampleListener
import com.liulishuo.filedownloader.FileDownloader
import com.liulishuo.filedownloader.util.FileDownloadUtils
import com.tencent.smtt.sdk.QbSdk
import com.tencent.smtt.sdk.ValueCallback
import com.xty.base.act.BaseListAct
import com.xty.common.LogUtils
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.audioUtils.RecordAudio
import com.xty.common.util.OpenFilesUtils
import com.xty.network.model.EnterpriseInfoHomeBean
import com.zj365.mime.BuildConfig
import com.zj365.mime.adapter.EnterpriseInformationChildAdapter
import com.zj365.mime.databinding.ActEnterpriseInformationTypeBinding
import com.zj365.mime.vm.EnterpriseInformationVm
import java.io.File

@Route(path = ARouterUrl.ENTERPRISE_TYPE_INFO)
class EnterpriseInformationForTypeAct : BaseListAct<EnterpriseInformationVm>() {

    val binding by lazy { ActEnterpriseInformationTypeBinding.inflate(layoutInflater) }

    val mAdapter by lazy { EnterpriseInformationChildAdapter(1) }

    var type = 0

    val audioUtils by lazy { RecordAudio() }

    var isAudioPlay = false

    override fun initAdapter() {
        binding.mRecycle.adapter = mAdapter
        binding.mRecycle.layoutManager = LinearLayoutManager(this@EnterpriseInformationForTypeAct)

        mAdapter.setOnItemClickListener { adapter, view, position ->
            var bean = adapter.data[position] as EnterpriseInfoHomeBean.EnterpriseInfoChildBean

            when(bean.fileFormat){  //	格式1,word,2,pdf,3.文本，4.excel,5.音频。6.链接
                1->{
                    if (!TextUtils.isEmpty(bean.fileUrl)){
                       var filePath = FileDownloadUtils.getDefaultSaveRootPath() + File.separator + "zj365" + File.separator + "${bean.title}.word"
                        var file = File(filePath)
                        if (file.exists()){
                            file.delete()
                        }

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

                }

                2->{
                    if (!TextUtils.isEmpty(bean.fileUrl)){
                        var 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)){
                      var  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)){
                      var  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)){
                       var 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)){
                        bundle.clear()
                        bundle.putString(
                            "web_site_url",
                            "${bean.fileUrl}"
                        )
                        bundle.putBoolean("isShowTitle",true)
                        bundle.putString("title",bean.title)
                        RouteManager.goAct(ARouterUrl.H5_ACT,bundle)
                    }
                }
            }
        }

        setRecycleRefresh(binding.mRecycle,binding.mRefresh,true)
    }


    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = "企业资料"
        binding.title.mIvBack.setOnClickListener {
            finish()
        }
    }


    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)
                    loadingView.show()
                    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)
                    loadingView.dismiss()
                    OpenFilesUtils.openFile(this@EnterpriseInformationForTypeAct, File(filePath))
                }

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

    override fun onDestroy() {
        audioUtils.stopPlay()
        super.onDestroy()
    }

    override fun loadData() {
        type = intent.getIntExtra("type",0)
        mViewModel.getEnterpriseInformationForType(type,page)
    }

    override fun liveObserver() {
        mViewModel.enterpriseInfoForTypeLiveData.observe(this){
            setDate(mAdapter,it.data.list)
        }
    }

    override fun setLayout() = binding.root
}