package com.base.appzxhy.ui.play

import android.annotation.SuppressLint
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.view.Gravity
import androidx.appcompat.widget.AppCompatTextView
import androidx.core.content.ContextCompat
import com.base.appzxhy.R
import com.base.appzxhy.utils.LogEx
import com.bytedance.sdk.shortplay.api.PSSDK
import com.bytedance.sdk.shortplay.api.PSSDK.ControlStatus
import com.bytedance.sdk.shortplay.api.PSSDK.IControlStatusView
import com.bytedance.sdk.shortplay.api.ShortPlay
import com.bytedance.sdk.shortplay.api.ShortPlayFragment

class CustomLikeView : AppCompatTextView, IControlStatusView {

    private val TAG = "CustomLikeView"

    private var likeDrawable: Drawable?
    private var likedDrawable: Drawable?
    private var status = ControlStatus.Normal

    constructor(context: Context) : super(context) {
        gravity = Gravity.CENTER_HORIZONTAL
        setTextColor(Color.parseColor("#ffffff"))
        textSize = 12f
        likeDrawable = ContextCompat.getDrawable(context, R.mipmap.fy_dianzan_n)
        likedDrawable = ContextCompat.getDrawable(context, R.mipmap.fy_dianzan_s)

        val size = resources.getDimensionPixelSize(R.dimen.dp_32)
        likeDrawable?.setBounds(0, 0, size, size)
        likedDrawable?.setBounds(0, 0, size, size)
    }


    override fun getControlViewType(): PSSDK.ControlViewType {
        return PSSDK.ControlViewType.Like
    }

    override fun bindItemData(fragment: ShortPlayFragment?, shortPlay: ShortPlay, index: Int) {
    }

    @SuppressLint("SetTextI18n")
    override fun setCurrentStatus(shortPlay: ShortPlay, index: Int, status: ControlStatus, extraInfo: PSSDK.StatusExtraInfo) {
        this.status = status
        LogEx.logDebug(TAG, "setCurrentStatus status=$status")
        setCompoundDrawables(null, if (status == ControlStatus.Normal) likeDrawable else likedDrawable, null, null)

        text = extraInfo.totalCollectCount.toString()
    }

    override fun getCurrentStatus(shortPlay: ShortPlay, index: Int): ControlStatus {
        return status
    }

    override fun onClicked(shortPlay: ShortPlay, index: Int, status: ControlStatus): ControlStatus {
        return if (status == ControlStatus.Normal) ControlStatus.Selected else ControlStatus.Normal
    }

}