package com.base.filerecoveryrecyclebin.activity.recyclebin

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Build
import android.view.View
import android.widget.Toast
import androidx.activity.addCallback
import androidx.annotation.RequiresApi
import androidx.core.content.FileProvider
import com.base.filerecoveryrecyclebin.activity.ResultActivity
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.findRecycleBinFile
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.findRecycleBinJsonFile
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECYCLE_BIN
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.databinding.ActivityRecycleBinDetailBinding
import com.base.filerecoveryrecyclebin.help.BaseActivity
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatSize
import com.base.filerecoveryrecyclebin.help.KotlinExt.toFormatTime
import com.base.filerecoveryrecyclebin.help.VideoPlayerUtil
import com.base.filerecoveryrecyclebin.utils.BarUtils
import com.base.filerecoveryrecyclebin.utils.LogEx
import com.bumptech.glide.Glide
import com.google.gson.Gson
import java.io.File


class RecycleBinDetailActivity : BaseActivity<ActivityRecycleBinDetailBinding>() {

    private val TAG = "RecycleBinDetailActivity"
    override val binding: ActivityRecycleBinDetailBinding by lazy {
        ActivityRecycleBinDetailBinding.inflate(layoutInflater)
    }
    private var recycleBinBean: RecycleBinBean? = null

    @SuppressLint("SetTextI18n")
    @RequiresApi(Build.VERSION_CODES.Q)
    override fun initView() {

        BarUtils.setStatusBarLightMode(this, true)
        BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//        binding.root.updatePadding(top = BarUtils.getStatusBarHeight())

        val json = intent.extras?.getString("Json") ?: ""
        LogEx.logDebug(TAG, "json=$json")
        recycleBinBean = Gson().fromJson(json, RecycleBinBean::class.java)
        recycleBinBean?.let { bean ->
            if (bean.mimeType.contains("image /")) {
                binding.ivBofang.visibility = View.GONE
            }
            if (bean.mimeType.contains("video/")) {
                binding.ivBofang.visibility = View.VISIBLE
            }

            val file = File(bean.path)
            val binFile = findRecycleBinFile(file.name)
            val binJsonFile = findRecycleBinJsonFile(file.name)
            if (binFile != null && binJsonFile != null) {
                Glide.with(this).load(binFile.absoluteFile).centerCrop().into(binding.iv)

                binding.tvName.text = bean.path
                binding.tvSize.text = bean.size.toFormatSize()
                binding.tvTime.text = "Delete time:${bean.deleteTime.toFormatTime()}"

            }
        }
    }

    override fun initListener() {
        super.initListener()
        onBackPressedDispatcher.addCallback {
            finish()
        }
        binding.flBack.setOnClickListener {
            onBackPressedDispatcher.onBackPressed()
        }
        binding.tvRestore.setOnClickListener {
            recycleBinBean?.let { bean ->
                val file = File(bean.path)
                val binFile = findRecycleBinFile(file.name)
                val binJsonFile = findRecycleBinJsonFile(file.name)
                runCatching {
                    binFile?.copyTo(File(bean.path), true)
                    binFile?.delete()
                    binJsonFile?.delete()
                }
            }
            startActivity(Intent(this, ResultActivity::class.java).apply {
                putExtra("from", RECYCLE_BIN)
            })
            finish()
        }

        binding.ivBofang.setOnClickListener {
            recycleBinBean?.let { bean ->
                val file = File(bean.path)
                val binFile = findRecycleBinFile(file.name)
                 VideoPlayerUtil.playVideo(this,binFile?.absolutePath)
            }

        }
    }

}