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 CustomCollectView : AppCompatTextView, IControlStatusView {

    private val TAG = "CustomCollectView"

    private var collectDrawable: Drawable?
    private var collectedDrawable: Drawable?
    private var status = ControlStatus.Normal

    constructor(context: Context) : super(context) {
        gravity = Gravity.CENTER_HORIZONTAL
        setTextColor(Color.parseColor("#ffffff"))
        textSize = 12f
        collectDrawable = ContextCompat.getDrawable(context, R.mipmap.fy_shoucang_n)
        collectedDrawable = ContextCompat.getDrawable(context, R.mipmap.fy_shoucang_s)

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


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

    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) collectDrawable else collectedDrawable, 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
    }

}