package com.remax.visualnovel.widget.imagepicker.views.wrapper

import android.content.Context
import android.view.View
import androidx.core.view.isVisible
import com.remax.visualnovel.R
import com.remax.visualnovel.databinding.PickerRedbookTitlebarBinding
import com.remax.visualnovel.extension.navRotationOpen
import com.remax.visualnovel.extension.setOnClick
import com.remax.visualnovel.widget.imagepicker.bean.ImageItem
import com.remax.visualnovel.widget.imagepicker.bean.ImageSet
import com.remax.visualnovel.widget.imagepicker.bean.selectconfig.BaseSelectConfig
import com.remax.visualnovel.widget.imagepicker.views.base.PickerControllerView
import com.remax.visualnovel.widget.uitoken.changeTextColor
import com.remax.visualnovel.widget.uitoken.handleUIToken

/**
 * Created by HJW on 2022/10/24
 */
class TitleBar constructor(context: Context?) : PickerControllerView(context) {

    private var binding: PickerRedbookTitlebarBinding? = null

    override fun getLayoutId(): Int {
        return R.layout.picker_redbook_titlebar
    }

    override fun initView(view: View?) {
        view?.run {
            binding = PickerRedbookTitlebarBinding.bind(this.findViewById(R.id.titleBar))
            binding?.also {
                val bgColor = context.handleUIToken(R.string.color_background_specialmap)?.color ?: 0
                it.mTitleContainer.navBg.setBackgroundColor(bgColor)
                it.mTitleContainer.navBack.setText(R.string.icon_close)
                it.mTitleContainer.tvTitleArrow.isVisible = true
                setOnClick(it.mTitleContainer.navBack) {
                    onBackPressed()
                }
            }
        }
    }

    override fun getViewHeight(): Int {
        return context.resources.getDimensionPixelSize(R.dimen.nav_height)
    }

    override fun getCanClickToCompleteView(): View? {
        return binding?.mTitleContainer?.rightConfirmBtn
    }

    override fun getCanClickToIntentPreviewView(): View? {
        return null
    }

    override fun getCanClickToToggleFolderListView(): View? {
        return binding?.mTitleContainer?.tvTitleLayout
    }

    override fun setTitle(title: String?) {
        binding?.mTitleContainer?.also {
            it.tvTitle.text = title
        }
    }

    override fun onTransitImageSet(isOpen: Boolean) {
        binding?.run {
            mTitleContainer.tvTitleArrow.navRotationOpen(isOpen)
            mTitleContainer.tvTitle.changeTextColor {
                textUIColorToken = context.getString(if (isOpen) R.string.color_primary_variant_normal else R.string.color_txt_primary_normal)
            }
            mTitleContainer.tvTitleArrow.changeTextColor {
                textUIColorToken = context.getString(if (isOpen) R.string.color_primary_variant_normal else R.string.color_txt_secondary_normal)
            }
        }
    }

    override fun onImageSetSelected(imageSet: ImageSet?) {
        binding?.mTitleContainer?.also {
            it.tvTitle.text = imageSet?.name
        }
    }

    override fun refreshCompleteViewState(selectedList: ArrayList<ImageItem>?, selectConfig: BaseSelectConfig?) {
        binding?.also {
            if ((selectConfig?.maxCount ?: 0) <= 1 && selectConfig?.isSinglePickAutoComplete == true) {
                it.mTitleContainer.rightConfirmBtn.isVisible = false
            } else {
                it.mTitleContainer.rightConfirmBtn.isVisible = true
                it.mTitleContainer.rightConfirmBtn.isEnabled = !selectedList.isNullOrEmpty()
            }
        }
    }

}