package com.hanwha.morakmorak.ui.main.home

import android.os.Build
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.recyclerview.widget.RecyclerView
import com.hanwha.morakmorak.R
import com.hanwha.morakmorak.data.remote.model.Home
import com.yalantis.phoenix.PullToRefreshView
import timber.log.Timber

/**
 * Created by aaron on 06/09/2019
 */


@BindingAdapter(value = ["viewModel", "items"])
fun setMeetings(view: RecyclerView, vm: HomeViewModel, items: List<Home>) {
    Timber.i("setMeetings")
    view.adapter?.run {
        if (this is HomeAdapter) {
            Timber.i("updateAdapter")
            this.items = items
            this.notifyDataSetChanged()
        }
    } ?: run {
        Timber.i("makeAdpater")
        HomeAdapter(vm, items).apply { view.adapter = this }
    }
}

@BindingAdapter("refreshListener")
fun setRefreshListener(view: PullToRefreshView, vm: HomeViewModel) {
    view.setOnRefreshListener(vm.refreshListener)
}

@BindingAdapter("dateText")
fun setDateText(view: TextView, date: String) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        if (date == "오늘") {
            view.setTextColor(view.context.getColor(R.color.orange_pink_two))
        } else {
            view.setTextColor(view.context.getColor(R.color.black))
        }
    }
    view.text = date
}
