package com.adealink.weparty.room.minimize.view

import android.animation.ObjectAnimator
import android.animation.ValueAnimator
import android.view.LayoutInflater
import android.view.View
import android.view.animation.LinearInterpolator
import com.adealink.frame.util.DisplayUtil
import com.adealink.frame.util.onClick
import com.adealink.weparty.commonui.ext.dp
import com.adealink.weparty.commonui.widget.floatview.view.BaseDragFloatView
import com.adealink.weparty.room.databinding.LayoutMinimizedRoomFloatViewBinding
import com.adealink.weparty.room.sdk.service.roomService


class MinimizedRoomFloatView(floatData: MinimizedRoomFloatData) : BaseDragFloatView(floatData) {

    private var binding: LayoutMinimizedRoomFloatViewBinding? = null
    private var closeClick: (() -> Unit)? = null
    private var roomClick: (() -> Unit)? = null
    private var rotatingAnim: ObjectAnimator? = null

    override fun onCreate() {
        super.onCreate()
        val binding = LayoutMinimizedRoomFloatViewBinding.inflate(LayoutInflater.from(context))
        this.binding = binding
        setContentView(binding.root)
        binding.clBg.onClick {
            roomClick?.invoke()
        }
        binding.btnClose.onClick {
            closeClick?.invoke()
        }
        roomService.joinController.joinedRoomInfo?.let {
            binding.ivAvatar.setImageUrl(it.coverURL)
        }
        rotatingAnim =
            ObjectAnimator.ofFloat(binding.ivAvatar, "rotation", 0f, 360f)
                .setDuration(4000L)
        rotatingAnim?.interpolator = LinearInterpolator()
        rotatingAnim?.repeatCount = ValueAnimator.INFINITE
    }

    override fun getLayoutParamWidth(): Int {
        return 86.dp()
    }

    override fun getLayoutParamHeight(): Int {
        return 90.dp()
    }

    override fun getClickableViews(): List<View> {
        val clickViews = mutableListOf<View>()
        binding?.btnClose?.let {
            clickViews.add(it)
        }
        binding?.clBg?.let {
            clickViews.add(it)
        }
        return clickViews
    }

    override fun getLayoutParamX(): Int {
        return DisplayUtil.getScreenWidth() - getLayoutParamWidth()
    }

    override fun getLayoutParamY(): Int {
        return DisplayUtil.getScreenHeight() / 3 * 2
    }

    override fun applySnapToEdge(): Boolean {
        return true
    }

    fun setOnCloseClick(click: (() -> Unit)?) {
        this.closeClick = click
    }

    fun setOnRoomClick(click: (() -> Unit)?) {
        this.roomClick = click
    }

    fun show() {
        visibility = View.VISIBLE
        rotatingAnim?.cancel()
        rotatingAnim?.start()
    }

    fun hide() {
        visibility = View.GONE
        rotatingAnim?.cancel()
    }

    override fun onDestroy() {
        super.onDestroy()
        rotatingAnim?.cancel()
    }
}