package com.base.filerecoveryrecyclebin.activity.recyclebin

import android.annotation.SuppressLint
import android.content.Intent
import android.graphics.Color
import android.os.Build
import android.util.Log
import android.view.View
import androidx.activity.addCallback
import androidx.annotation.RequiresApi
import androidx.core.view.isVisible
import androidx.core.view.updatePadding
import com.base.filerecoveryrecyclebin.R
import com.base.filerecoveryrecyclebin.activity.ResultActivity
import com.base.filerecoveryrecyclebin.activity.recovery.FileRecoveredActivity
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.findRecycleBinFile
import com.base.filerecoveryrecyclebin.activity.recyclebin.RecycleBinFileEx.findRecycleBinJsonFile
import com.base.filerecoveryrecyclebin.bean.ConstObject
import com.base.filerecoveryrecyclebin.bean.ConstObject.RECYCLE_BIN
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean.Companion.mAudioStr
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean.Companion.mDocumentStr
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean.Companion.mImageStr
import com.base.filerecoveryrecyclebin.bean.RecycleBinBean.Companion.mVideoStr
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.service.BackUpUtils.recoveryFile
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() {

        val json = intent.extras?.getString("Json") ?: ""
        LogEx.logDebug(TAG, "json=$json")
        recycleBinBean = Gson().fromJson(json, RecycleBinBean::class.java)
        recycleBinBean?.let { bean ->
            if (mImageStr.contains("." + bean.mimeType)) {
                BarUtils.setStatusBarLightMode(this, true)
                BarUtils.setStatusBarColor(this, Color.WHITE)
                binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
                binding.ivBofang.visibility = View.GONE
            }
            if (mVideoStr.contains("." + bean.mimeType)) {
                BarUtils.setStatusBarLightMode(this, true)
                BarUtils.setStatusBarColor(this, Color.WHITE)
                binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
                binding.ivBofang.visibility = View.VISIBLE
            }
            if (mAudioStr.contains("." + bean.mimeType)) {
                BarUtils.setStatusBarLightMode(this, false)
                BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
//                binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
                binding.flTop.setBackgroundColor(Color.TRANSPARENT)
                binding.root.setBackgroundResource(R.mipmap.bg_music)
                binding.ivIcon.isVisible = true
                binding.ivIcon.setImageResource(R.mipmap.yuan_music)
            }
            if (mDocumentStr.contains("." + bean.mimeType)) {
                BarUtils.setStatusBarLightMode(this, false)
                BarUtils.setStatusBarColor(this, Color.TRANSPARENT)
                binding.root.updatePadding(top = BarUtils.getStatusBarHeight())
                binding.flTop.setBackgroundColor(Color.TRANSPARENT)
                binding.root.setBackgroundColor(Color.parseColor("#EDEDEE"))
                binding.ivBofang.visibility = View.GONE
                binding.ivIcon.isVisible = true
                binding.ivIcon.setImageResource(R.mipmap.wendang)
            }

            Glide.with(this).load(bean.backupPath).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 = File(bean.backupPath)
                runCatching {
                    binFile.copyTo(file, true)
                    binFile.delete()
                    recoveryFile(bean.name)
                }
            }
            startActivity(Intent(this, FileRecoveredActivity::class.java).apply {
                recycleBinBean?.let { bean ->
                    if (mImageStr.contains("." + bean.mimeType)) {
                        putExtra("ScanType", ConstObject.SCAN_PHOTOS)
                    }
                    if (mVideoStr.contains("." + bean.mimeType)) {
                        putExtra("ScanType", ConstObject.SCAN_VIDEOS)
                    }
                    if (mAudioStr.contains("." + bean.mimeType)) {
                        putExtra("ScanType", ConstObject.SCAN_AUDIOS)
                    }
                    if (mDocumentStr.contains("." + bean.mimeType)) {
                        putExtra("ScanType", ConstObject.SCAN_DOCUMENTS)
                    }
                    putExtra("Path", bean.path)
                }
                putExtra("Number", 1)
            })
            finish()
        }

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

        }
        binding.flDelete.setOnClickListener {
            recycleBinBean?.let { bean ->
                val file = File(bean.path)
                val binFile = findRecycleBinFile(file.name)
                val binJsonFile = findRecycleBinJsonFile(file.name)

                binFile?.delete()
                binJsonFile?.delete()
            }
            finish()
        }
    }


}