package com.lukouguoji.gjc.activity

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.AnimatorSet
import android.animation.ObjectAnimator
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.View
import com.alibaba.android.arouter.facade.annotation.Route
import com.lukouguoji.gjc.R
import com.lukouguoji.gjc.databinding.ActivityGjcWeighingStartBinding
import com.lukouguoji.gjc.viewModel.GjcWeighingStartViewModel
import com.lukouguoji.module_base.base.BaseBindingActivity
import com.lukouguoji.module_base.common.Constant
import com.lukouguoji.module_base.router.ARouterConstants
import com.lukouguoji.module_base.util.DeviceUtil

/**
 * 国际出港计重-开始计重页面
 */
@Route(path = ARouterConstants.ACTIVITY_URL_GJC_WEIGHING_START)
class GjcWeighingStartActivity :
    BaseBindingActivity<ActivityGjcWeighingStartBinding, GjcWeighingStartViewModel>() {

    private var isExpanded = false

    override fun layoutId() = R.layout.activity_gjc_weighing_start

    override fun viewModelClass() = GjcWeighingStartViewModel::class.java

    override fun initOnCreate(savedInstanceState: Bundle?) {
        setBackArrow(if (DeviceUtil.isPhone()) "开始计重" else "国际出港开始计重")
        binding.viewModel = viewModel

        // 手机版专属：提交体携带托盘数量/托盘自重（平板不调用，请求体与改造前完全一致）
        if (DeviceUtil.isPhone()) {
            viewModel.initPhoneExtras()
        }

        viewModel.initOnCreated(this, intent)

        setupFloatButtons()
    }

    private fun setupFloatButtons() {
        // 浮动按钮组仅平板布局存在（layout-sw600dp），手机布局无这些 id，直接跳过
        val mainFloatButton = binding.mainFloatButton ?: return
        val maskView = binding.maskView ?: return
        val subButton1 = binding.subButton1 ?: return
        val subButton2 = binding.subButton2 ?: return

        // 主按钮点击事件
        mainFloatButton.setOnClickListener {
            if (!isExpanded) {
                expandButtons()
            }
        }

        // 遮罩层点击事件
        maskView.setOnClickListener {
            collapseButtons()
        }

        // 子按钮1点击事件 - 跳转到出港运抵页面
        subButton1.setOnClickListener {
            com.alibaba.android.arouter.launcher.ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_INT_EXP_ARRIVE)
                .navigation()
            collapseButtons()
        }

        // 子按钮2点击事件 - 跳转到计重记录
        subButton2.setOnClickListener {
            com.alibaba.android.arouter.launcher.ARouter.getInstance()
                .build(ARouterConstants.ACTIVITY_URL_GJC_WEIGHING_RECORD_LIST)
                .navigation()
            collapseButtons()
        }
    }

    /**
     * 展开子按钮（仅平板，手机布局无浮动按钮组）
     */
    private fun expandButtons() {
        val mainFloatButton = binding.mainFloatButton ?: return
        val maskView = binding.maskView ?: return
        val subButton1 = binding.subButton1 ?: return
        val subButton2 = binding.subButton2 ?: return

        isExpanded = true

        // 显示遮罩层
        maskView.visibility = View.VISIBLE
        maskView.alpha = 0f
        maskView.animate().alpha(0.3f).setDuration(200).start()

        // 显示子按钮
        subButton1.visibility = View.VISIBLE
        subButton2.visibility = View.VISIBLE

        // 子按钮1动画（向左上方移动: 左移15dp, 上移55dp）
        val sub1TransX = ObjectAnimator.ofFloat(subButton1, "translationX", 0f, -5f)
        val sub1TransY = ObjectAnimator.ofFloat(subButton1, "translationY", 0f, -25f)
        val sub1Alpha = ObjectAnimator.ofFloat(subButton1, "alpha", 0f, 1f)
        val sub1Scale = ObjectAnimator.ofFloat(subButton1, "scaleX", 0f, 1f)
        val sub1ScaleY = ObjectAnimator.ofFloat(subButton1, "scaleY", 0f, 1f)

        val animSet1 = AnimatorSet()
        animSet1.playTogether(sub1TransX, sub1TransY, sub1Alpha, sub1Scale, sub1ScaleY)
        animSet1.duration = 200

        // 子按钮2动画（向左下方移动: 左移55dp, 下移15dp）
        val sub2TransX = ObjectAnimator.ofFloat(subButton2, "translationX", 0f, -25f)
        val sub2TransY = ObjectAnimator.ofFloat(subButton2, "translationY", 0f, -5f)
        val sub2Alpha = ObjectAnimator.ofFloat(subButton2, "alpha", 0f, 1f)
        val sub2Scale = ObjectAnimator.ofFloat(subButton2, "scaleX", 0f, 1f)
        val sub2ScaleY = ObjectAnimator.ofFloat(subButton2, "scaleY", 0f, 1f)

        val animSet2 = AnimatorSet()
        animSet2.playTogether(sub2TransX, sub2TransY, sub2Alpha, sub2Scale, sub2ScaleY)
        animSet2.duration = 200
        animSet2.startDelay = 50

        // 主按钮隐藏动画
        mainFloatButton.animate()
            .scaleX(0f)
            .scaleY(0f)
            .alpha(0f)
            .setDuration(150)
            .setListener(object : AnimatorListenerAdapter() {
                override fun onAnimationEnd(animation: Animator) {
                    mainFloatButton.visibility = View.GONE
                }
            })
            .start()

        animSet1.start()
        animSet2.start()
    }

    /**
     * 收起子按钮（仅平板，手机布局无浮动按钮组）
     */
    private fun collapseButtons() {
        val mainFloatButton = binding.mainFloatButton ?: return
        val maskView = binding.maskView ?: return
        val subButton1 = binding.subButton1 ?: return
        val subButton2 = binding.subButton2 ?: return

        isExpanded = false

        // 隐藏遮罩层
        maskView.animate().alpha(0f).setDuration(200).withEndAction {
            maskView.visibility = View.GONE
        }.start()

        // 子按钮1动画（回到原位）
        val sub1TransX = ObjectAnimator.ofFloat(subButton1, "translationX", subButton1.translationX, 0f)
        val sub1TransY = ObjectAnimator.ofFloat(subButton1, "translationY", subButton1.translationY, 0f)
        val sub1Alpha = ObjectAnimator.ofFloat(subButton1, "alpha", 1f, 0f)
        val sub1Scale = ObjectAnimator.ofFloat(subButton1, "scaleX", 1f, 0f)
        val sub1ScaleY = ObjectAnimator.ofFloat(subButton1, "scaleY", 1f, 0f)

        val animSet1 = AnimatorSet()
        animSet1.playTogether(sub1TransX, sub1TransY, sub1Alpha, sub1Scale, sub1ScaleY)
        animSet1.duration = 200

        // 子按钮2动画（回到原位）
        val sub2TransX = ObjectAnimator.ofFloat(subButton2, "translationX", subButton2.translationX, 0f)
        val sub2TransY = ObjectAnimator.ofFloat(subButton2, "translationY", subButton2.translationY, 0f)
        val sub2Alpha = ObjectAnimator.ofFloat(subButton2, "alpha", 1f, 0f)
        val sub2Scale = ObjectAnimator.ofFloat(subButton2, "scaleX", 1f, 0f)
        val sub2ScaleY = ObjectAnimator.ofFloat(subButton2, "scaleY", 1f, 0f)

        val animSet2 = AnimatorSet()
        animSet2.playTogether(sub2TransX, sub2TransY, sub2Alpha, sub2Scale, sub2ScaleY)
        animSet2.duration = 200

        animSet1.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                subButton1.visibility = View.GONE
            }
        })

        animSet2.addListener(object : AnimatorListenerAdapter() {
            override fun onAnimationEnd(animation: Animator) {
                subButton2.visibility = View.GONE
            }
        })

        // 主按钮显示动画
        mainFloatButton.visibility = View.VISIBLE
        mainFloatButton.animate()
            .scaleX(1f)
            .scaleY(1f)
            .alpha(1f)
            .setDuration(200)
            .setStartDelay(100)
            .setListener(null)
            .start()

        animSet1.start()
        animSet2.start()
    }

    companion object {
        /**
         * 启动开始计重页面
         * @param context 上下文
         * @param maWbId 运单ID，默认为0（新增模式）
         */
        @JvmStatic
        fun startForAdd(context: Context, maWbId: Long = 0) {
            val starter = Intent(context, GjcWeighingStartActivity::class.java)
                .putExtra(Constant.Key.MAWB_ID, maWbId)
            context.startActivity(starter)
        }
    }
}
