package com.assimilate.alltrans.curview

import android.app.PendingIntent
import android.content.Context
import android.content.Intent
import android.graphics.PixelFormat
import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.view.WindowManager
import com.assimilate.alltrans.common.FirebaseAnalyticsHelper
import com.assimilate.alltrans.databinding.SusControlViewBinding
import com.assimilate.alltrans.viewui.MainActivity
import com.assimilate.alltrans.viewui.PhotoImageActivity
import com.assimilate.alltrans.viewui.WelActivity

class ControlView(private val context: Context) {

    interface ControlViewListener {
        fun onGlobalClick()
        fun onCopyClick()
        fun onDistrictClick()
        fun onChangeLan()
    }

    private lateinit var binding: SusControlViewBinding
    private lateinit var controlView: View
    private var isAdded: Boolean = false
    private lateinit var fullScreenView: View
    private var listener: ControlViewListener? = null

    private val windowManager: WindowManager =
        context.getSystemService(Context.WINDOW_SERVICE) as WindowManager

    fun setControlViewListener(listener: ControlViewListener) {
        this.listener = listener
    }

    fun addControlView() {
        if (isAdded) return

        binding = SusControlViewBinding.inflate(LayoutInflater.from(context))
        controlView = binding.root

        // 添加全屏透明视图,点击区域外关闭
        fullScreenView = View(context)
        val fullScreenLayoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.MATCH_PARENT,
            WindowManager.LayoutParams.MATCH_PARENT,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                WindowManager.LayoutParams.TYPE_PHONE
            },
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
        )

        fullScreenLayoutParams.gravity = Gravity.TOP or Gravity.LEFT
        windowManager.addView(fullScreenView, fullScreenLayoutParams)

        val layoutParams = WindowManager.LayoutParams(
            WindowManager.LayoutParams.WRAP_CONTENT,
            WindowManager.LayoutParams.WRAP_CONTENT,
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
            } else {
                WindowManager.LayoutParams.TYPE_PHONE
            },
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT
        )
        layoutParams.gravity = Gravity.TOP or Gravity.LEFT
        layoutParams.x = 0
        layoutParams.y = 100

        windowManager.addView(controlView, layoutParams)
        FirebaseAnalyticsHelper.hoverScreenImpEvent()
        isAdded = true

        initControlClick()

        // 设置触摸监听器
        fullScreenView.setOnTouchListener { _, event ->
            if (event.action == MotionEvent.ACTION_DOWN) {
                removeControlView()
                fullScreenView.performClick()
                true
            } else {
                false
            }

        }


    }

    private fun initControlClick() {

        binding.ivSusChange.setOnClickListener {
            binding.susControlRoot.visibility = View.GONE
            listener?.onChangeLan()


        }
        binding.tvSusGlobal.setOnClickListener {
            binding.susControlRoot.visibility = View.GONE
            listener?.onGlobalClick()
        }

        binding.tvSusCopy.setOnClickListener {
            Log.d("ControlViewManager", "Copy text clicked")
            binding.susControlRoot.visibility = View.GONE
            listener?.onCopyClick()
        }

        binding.tvSusDistrict.setOnClickListener {
            binding.susControlRoot.visibility = View.GONE
            Log.d("ControlViewManager", "District translation clicked")
            listener?.onDistrictClick()
        }

        binding.tvSusPhoto.setOnClickListener {
            val intentPh = Intent(context, PhotoImageActivity::class.java)
            intentPh.putExtra("sus_ph", "sus_ph")
            val pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intentPh,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            pendingIntent.send()
            removeControlView()
        }

        binding.ivSusHome.setOnClickListener {
            val intentHome = Intent(context, MainActivity::class.java)
            intentHome.putExtra("sus_home","sus_home")
            val pendingIntent = PendingIntent.getActivity(
                context,
                0,
                intentHome,
                PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
            )
            pendingIntent.send()
            removeControlView()
        }
    }

    fun removeControlView() {
        if (isAdded) {
            windowManager.removeView(controlView)
            windowManager.removeView(fullScreenView)
            isAdded = false
        }
    }
}
