package com.apps.sketchmaster.view

import com.apps.sketchmaster.data.MainViewModel
import android.app.Dialog
import android.content.Intent
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.core.graphics.drawable.toDrawable
import androidx.fragment.app.DialogFragment
import androidx.lifecycle.ViewModelProvider
import com.apps.sketchmaster.utils.AllUtils
import com.apps.sketchmaster.databinding.DialogIntentBinding

class IntentDialog : DialogFragment() {
    private var _binding: DialogIntentBinding? = null
    private val binding get() = _binding!!
    private var path = ""
    private lateinit var mainViewModel: MainViewModel

    companion object {
        private const val PIC_KEY = "path"
        fun newInstance(imagePath: String): IntentDialog {
            val args = Bundle().apply {
                putString(PIC_KEY, imagePath)
            }
            return IntentDialog().apply { arguments = args }
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        _binding = DialogIntentBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        path = arguments?.getString(PIC_KEY) ?: ""
        val bitmap = AllUtils.switchTypeFromAssets(requireContext(), path)
        if (bitmap == null) {
            Log.d("YYYYYYYY", "return null")
            val decodedBitmap = BitmapFactory.decodeFile(path)
            binding.image.setImageBitmap(decodedBitmap)
            Log.d("YYYYYYYY", "return decodedBitmap$decodedBitmap")
        } else {
            binding.image.setImageBitmap(bitmap)
        }

//        binding.imgBack.setOnClickListener { dismiss() }
        mainViewModel = ViewModelProvider(requireActivity())[MainViewModel::class.java]
        mainViewModel.checkIsBase(path)
        mainViewModel.isBase.observe(viewLifecycleOwner) { b ->
            if (b != null) {
                binding.imgLike.isSelected=b
            }
        }
        binding.imgLike.setOnClickListener { v ->
            val selected = v.isSelected
            binding.imgLike.isSelected = !selected
            if (!selected) {
                mainViewModel.addBase(path)
            } else {
                mainViewModel.deleteBasePath(path)
            }
        }
        binding.rlBtn.setOnClickListener {
            val intent = Intent(requireActivity(), SketchStartActivity::class.java)
            intent.putExtra(SketchStartActivity.PATH_KEY,path)
            startActivity(intent)
            dismiss()
        }
    }

    override fun onDestroyView() {
        super.onDestroyView()
        _binding = null
    }

    // 设置弹窗居中、透明背景
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val dialog = super.onCreateDialog(savedInstanceState)
        dialog.window?.setBackgroundDrawable(Color.TRANSPARENT.toDrawable())
        return dialog
    }
}