package com.example.defenseapplication.liveVideo

import android.os.Bundle
import android.widget.RelativeLayout
import androidx.appcompat.app.AppCompatActivity
import com.example.defenseapplication.R
import com.example.defenseapplication.databinding.StrikePositionActivityBinding
import com.example.defenseapplication.view.CustomDialogFragment
import com.example.defenseapplication.view.showDefenseDialog
import com.gyf.immersionbar.ImmersionBar
//打击位置
class StrikePositionActivity : AppCompatActivity() {
    private lateinit var binding: StrikePositionActivityBinding
    private lateinit var markerLayoutParams: RelativeLayout.LayoutParams
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = StrikePositionActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)

        ImmersionBar.with(this)
            .statusBarDarkFont(true)
            .titleBar(binding.topBar)
            .init()

        binding.ivBack.setOnClickListener {
            finish()
        }

        binding.btnSubmit.setOnClickListener {
            showConfirmDialog()
        }
        // 获取准星的 LayoutParams，并移除居中约束
        markerLayoutParams = binding.ivStrikeMarker.layoutParams as RelativeLayout.LayoutParams
        markerLayoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL)
        markerLayoutParams.removeRule(RelativeLayout.CENTER_VERTICAL)
        binding.ivStrikeMarker.layoutParams = markerLayoutParams

        // 设置人体图片的点击监听
        binding.ivHumanBody.setOnTouchListener { _, event ->
            if (event.action == android.view.MotionEvent.ACTION_DOWN) {
                moveMarkerTo(event.x, event.y)
            }
            true
        }
        // 初始化准星位置：人体头部（假设在图片宽度的1/2，高度的1/5处）
        binding.ivHumanBody.post {
            val humanWidth = binding.ivHumanBody.width
            val humanHeight = binding.ivHumanBody.height
            if (humanWidth > 0 && humanHeight > 0) {
                val headX = humanWidth / 2f
                val headY = humanHeight / 5f
                moveMarkerTo(headX, headY)
            }
        }
    }

    private fun showConfirmDialog() {
        val config = CustomDialogFragment.Config(
            title = getString(R.string.tip),
            message = getString(R.string.confirm_modify_strike_pos),
            positiveText = getString(R.string.confirm),
            negativeText = getString(R.string.cancel)
        )
        showDefenseDialog(config, onPositiveClick = {
            finish()
        //            if(){
//                ToastUtils.showSuccess(this, getString(R.string.wifi_connection_success))
//            }else{
//                ToastUtils.showError(this, getString(R.string.wifi_connection_failed))
//            }
        })
    }
    private fun moveMarkerTo(x: Float, y: Float) {
        val marker = binding.ivStrikeMarker
        // 让准星中心对准点击位置
        val left = x - marker.width / 2f
        val top = y - marker.height / 2f

        markerLayoutParams.leftMargin = left.toInt()
        markerLayoutParams.topMargin = top.toInt()
        marker.layoutParams = markerLayoutParams
    }
}