package com.daodaoshi.chick_mood

import android.os.Bundle
import android.util.Log
import android.view.MotionEvent
import android.view.View
import android.widget.ImageView
import androidx.appcompat.app.AppCompatActivity
import com.daodaoshi.chick_mood.databinding.ActivityImagePreviewBinding
import com.daodaoshi.chick_mood.ImageUtils
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

/**
 * 图片预览Activity
 * 支持图片放大查看、手势缩放和拖拽
 */
class ImagePreviewActivity : AppCompatActivity() {

    private lateinit var binding: ActivityImagePreviewBinding
    private var currentScale = 1.0f
    private var startX = 0f
    private var startY = 0f
    private var translateX = 0f
    private var translateY = 0f

    companion object {
        private const val MAX_SCALE = 3.0f
        private const val MIN_SCALE = 1.0f
        private const val TAG = "ImagePreviewActivity"
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityImagePreviewBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initViews()
        setupGestures()
    }

    /**
     * 初始化视图
     */
    private fun initViews() {
        // 设置返回按钮点击事件
        binding.btnClose.setOnClickListener {
            finish()
        }

        // 设置背景点击关闭
        binding.root.setOnClickListener {
            finish()
        }

        // 获取图片路径并加载图片
        val imagePath = intent.getStringExtra("image_path")
        if (imagePath != null) {
            // 加载实际图片
            loadImage(imagePath)
        } else {
            finish() // 没有图片路径则关闭
        }
    }

    /**
     * 设置手势操作
     */
    private fun setupGestures() {
        binding.imagePreview.setOnTouchListener { view, event ->
            when (event.action and MotionEvent.ACTION_MASK) {
                MotionEvent.ACTION_DOWN -> {
                    startX = event.x - translateX
                    startY = event.y - translateY
                    true
                }
                MotionEvent.ACTION_MOVE -> {
                    translateX = event.x - startX
                    translateY = event.y - startY
                    updateImageTransform()
                    true
                }
                MotionEvent.ACTION_POINTER_DOWN -> {
                    // 双指缩放逻辑
                    true
                }
                MotionEvent.ACTION_UP -> {
                    true
                }
                else -> false
            }
        }
    }

    /**
     * 更新图片变换
     */
    private fun updateImageTransform() {
        binding.imagePreview.apply {
            translationX = translateX
            translationY = translateY
            scaleX = currentScale
            scaleY = currentScale
        }
    }

    /**
     * 加载并显示图片
     */
    private fun loadImage(imagePath: String) {
        CoroutineScope(Dispatchers.IO).launch {
            val bitmap = ImageUtils.loadBitmapFromPath(imagePath)

            withContext(Dispatchers.Main) {
                if (bitmap != null) {
                    binding.imagePreview.setImageBitmap(bitmap)
                    Log.d("ImagePreviewActivity", "预览页图片加载成功: $imagePath")
                } else {
                    // 如果加载失败，使用占位图
                    binding.imagePreview.setImageResource(R.drawable.ic_placeholder_image)
                    Log.w("ImagePreviewActivity", "预览页图片加载失败，使用占位图: $imagePath")
                }
            }
        }
    }

    /**
     * 处理双击缩放
     */
    private fun handleDoubleTap() {
        if (currentScale == MIN_SCALE) {
            currentScale = 2.0f
        } else {
            currentScale = MIN_SCALE
            translateX = 0f
            translateY = 0f
        }
        updateImageTransform()
    }

    override fun finish() {
        super.finish()
        // 添加退出动画
        overridePendingTransition(0, android.R.anim.fade_out)
    }
}