package com.swago.home.search

import android.graphics.Rect
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.alibaba.android.arouter.launcher.ARouter
import com.chad.library.adapter.base.BaseQuickAdapter
import com.swago.baseswago.PersonDataDFragment
import com.swago.baseswago.UserVm
import com.swago.baseswago.activity.BaseListActivity
import com.swago.baseswago.constant.ARouteConstant
import com.swago.baseswago.cusview.SwagoRecyclerView
import com.swago.baseswago.model.MomentModel
import com.swago.baseswago.model.home.HomeModel
import com.swago.baseswago.util.DpPxUtil
import com.swago.baseswago.util.NoDoubleClickListener
import com.swago.home.databinding.ActivitySearchBinding
import com.swago.home.innerhome.HomeAdapter

/**
 *@date 2021/12/1 19:32
 *description:
 */
class SearchActivity : BaseListActivity<ActivitySearchBinding, MomentModel>() {

    private val searchVm by viewModels<SearchVm>()
    private val userVm by viewModels<UserVm>()

    private var searchThing = "swago"
    private var position = -1

    override val smartRecyclerView: SwagoRecyclerView<MomentModel> by lazy {
        binding.swagoRv as SwagoRecyclerView<MomentModel>
    }
    override val adapter: BaseQuickAdapter<MomentModel, *> by lazy {
        SearchAdapter()
    }
    override val layoutManager: RecyclerView.LayoutManager by lazy {
        LinearLayoutManager(this)
    }
    override val loadData: (page: Int) -> Unit = {
        searchVm.searchUser(searchThing, it + 1)
    }


    override fun initViewData() {
        setBackView(binding.toolbar.ivBack)
        smartRecyclerView.recyclerView?.addItemDecoration(VerticalItemDecoration())
        binding.ivSearch.setOnClickListener(object:NoDoubleClickListener(){
            override fun onClick() {
                val search = binding.etInput.text.toString()
                if (search.isNotEmpty()){
                    searchThing = search
                    smartRecyclerView.startRefresh()
                }
            }
        })

        adapter.setOnItemChildClickListener { _, _, position ->
            this.position = position
            val isFollow = adapter.data[position].is_follow
            if (isFollow == 0){
                userVm.followUser(adapter.data[position].id,0)
            }else if (isFollow == 1){
                userVm.followUser(adapter.data[position].id,1)
            }
        }

        adapter.setOnItemClickListener { _, _, position ->
            if (adapter.data.size>position){
                if (adapter.data[position].user_broadcast_status == 1){
                    ARouter.getInstance()
                        .build(ARouteConstant.Room.user)
                        .withInt("position",position)
                        .withParcelableArrayList("data",adapter.data as ArrayList<MomentModel>)
                        .navigation()
                }else{
                    PersonDataDFragment.newInstance(adapter.data[position].id,
                        isAnchor = true,
                        inRoom = false
                    ).show(supportFragmentManager,"PersonDataDFragment")
                }
            }
        }
    }

    override fun initLiveData() {
        searchVm.searchResultLiveData.observe(this){
            if (it == null){
                smartRecyclerView.onFetchDataError()
            }else{
                smartRecyclerView.onFetchDataFinish(it,true)
            }
        }

        userVm.followStateLiveData.observe(this) {
            if (position != -1) {
                adapter.data[position].is_follow =
                    if (adapter.data[position].is_follow == 1) 0 else 1
                adapter.notifyItemRemoved(position)
            }

        }
    }


    inner class VerticalItemDecoration : RecyclerView.ItemDecoration() {
        override fun getItemOffsets(
            outRect: Rect,
            view: View,
            parent: RecyclerView,
            state: RecyclerView.State
        ) {
            val margin = DpPxUtil.dip2px(20f)
            outRect.set(0, margin, 0, 0)
        }
    }
    
}