package cn.muchinfo.rma.view.base.home

import android.graphics.Typeface
import android.text.TextUtils
import android.view.Gravity
import androidx.fragment.app.Fragment
import cn.muchinfo.rma.R
import cn.muchinfo.rma.global.data.NoticeData
import cn.muchinfo.rma.lifecycle.bindOptional
import cn.muchinfo.rma.view.autoWidget.autoSize
import cn.muchinfo.rma.view.autoWidget.textColorInt
import cn.muchinfo.rma.view.autoWidget.textSizeAuto
import mtp.polymer.com.autowidget.adapter.BaseViewHolder
import org.jetbrains.anko.*

class AnnouncementViewHolder(
    private val baseFragment: Fragment,
    private val viewModel: HomeViewModel
) : BaseViewHolder<NoticeData>(baseFragment.context!!) {
    override val itemSize: IntArray = intArrayOf(matchParent, autoSize(160))

    override fun _FrameLayout.createContentView() {
        verticalLayout {

            linearLayout {
                imageView {
                    data.bindOptional(context){
                        if (it?.msgtype == "1"){
                            imageResource = R.mipmap.main_announcement_logo
                        }else{
                            imageResource = R.mipmap.rma_message_img
                        }
                    }
                }.lparams(autoSize(56), autoSize(41)) {
                    marginStart = autoSize(32)
                }

                textView {
                    data.bindOptional(context){
                        text = it?.title
                    }
                    maxEms = 14
                    maxLines = 1
                    ellipsize = TextUtils.TruncateAt.END
                    typeface = Typeface.defaultFromStyle(Typeface.BOLD)
                    textColorInt = R.color.black
                    textSizeAuto = 34
                }.lparams(wrapContent, wrapContent) {
                    marginStart = autoSize(14)
                }

                view { }.lparams(0, autoSize(1), 1f)

                textView {
                    data.bindOptional(context){
                        text = it?.createtime
                    }
                    textColorInt = R.color.text_hit_color
                    textSizeAuto = 26
                }.lparams(wrapContent, wrapContent) {
                    gravity = Gravity.CENTER_VERTICAL
                    marginEnd = autoSize(32)
                }

            }.lparams(matchParent, wrapContent) {
                topMargin = autoSize(30)
            }

            textView {
                maxLines = 1
                ellipsize = TextUtils.TruncateAt.END
                data.bindOptional(context){
                    text = it?.content
                }
                textColorInt = R.color.text_hint_color_two
                textSizeAuto = 26
            }.lparams(matchParent, wrapContent) {
                marginStart = autoSize(102)
                topMargin = autoSize(20)
            }
            view { }.lparams(autoSize(1), 0, 1f)
            view {
                background = resources.getDrawable(R.color.main_hit_bg_color)
            }.lparams(matchParent, autoSize(1))
        }.lparams(matchParent, autoSize(160))
    }

}