package com.zj365.health.act

import android.os.Vibrator
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.ItemTouchHelper
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.xty.base.act.BaseListAct
import com.xty.common.UmengEventId
import com.xty.common.arouter.ARouterUrl
import com.xty.common.arouter.RouteManager
import com.xty.common.event.FamilyEvent
import com.xty.common.onEventObject
import com.xty.common.recycleUtils.ItemHelperCallBack
import com.xty.common.recycleUtils.RecycleItem
import com.xty.common.util.CommonToastUtils
import com.zj365.health.R
import com.zj365.health.adapter.FamilyAdapter
import com.zj365.health.databinding.ActFamilyBinding
import com.zj365.health.vm.FamilyVm
import com.zj365.health.weight.DialogTip
import com.zj365.health.weight.RelationDialog
import com.zj365.health.weight.RemarkDialog
import com.xty.network.model.FamilyBean
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

/**
 * 我的家人
 */
@Route(path = ARouterUrl.FAMILY)
class FamilyAct : BaseListAct<FamilyVm>() {
    val binding by lazy { ActFamilyBinding.inflate(layoutInflater) }
    val adapter by lazy { FamilyAdapter() }
    val relationDialog by lazy {
        RelationDialog(this) {
//            mViewModel.changeFamilyNickName()
        }
    }
    var unBindId = ""

    val dialog by lazy {
        DialogTip(this, "是否确认解绑") {
            mViewModel.familyCancel(unBindId)
        }
    }

    override fun setLayout() = binding.root

    override fun initView() {
        super.initView()
        statusBar(binding.title.mView)
        binding.title.mTvTitle.text = resources.getString(R.string.intera_3)
        binding.title.mIvBack.apply {
            this.setOnClickListener {
                finish()
            }
        }
        binding.llAdd.setOnClickListener {
            RouteManager.goActRequest(ARouterUrl.BIND_FAMILY, this@FamilyAct, 1000)
        }
//        binding.title.mIvReight.apply {
////            visibility = View.VISIBLE
////            setImageResource(R.mipmap.icon_add)
//            setOnClickListener {
//
//            }
//        }
        setRecycleRefresh(binding.mRecycle, binding.mRefresh)
        onEventObject(baseContext, UmengEventId.MyFamily)
        //mViewModel.getFamilyRelationList()
    }

    override fun initAdapter() {
        binding.mRecycle.addItemDecoration(RecycleItem(this, 0))
        binding.mRecycle.layoutManager = LinearLayoutManager(this)
        binding.mRecycle.adapter = adapter

        var dragItem= ItemHelperCallBack({fromPosition,toPosition->
            var bean= adapter.data[fromPosition]
            adapter.data.remove(bean)
            adapter.data.add(toPosition,bean)
            adapter.notifyItemMoved(fromPosition,toPosition)
        },{
            when(it) {
                ItemTouchHelper.ACTION_STATE_DRAG -> {
                    val vib: Vibrator? = getSystemService(VIBRATOR_SERVICE) as Vibrator?
                    //震动70毫秒
                    //震动70毫秒
                    vib!!.vibrate(70)
                }
            }
        },{
          //移动结束
          mViewModel.changeFamilySort(adapter.data)
        },ContextCompat.getDrawable(this,R.drawable.shape_round_white))

        var helper=  ItemTouchHelper(dragItem)
        helper  .attachToRecyclerView(binding.mRecycle)
        adapter.addChildClickViewIds(R.id.mCancel, R.id.mEditName)
        adapter.setOnItemClickListener { adapter, view, position ->
            bundle.clear()
            bundle.putSerializable("bean", adapter.getItem(position) as FamilyBean)
            RouteManager.goAct(ARouterUrl.FRIEND_WATCH,bundle)
        }
        adapter.setOnItemChildClickListener { adapter, view, position ->
            val item = adapter.getItem(position) as FamilyBean
            when (view.id) {
                R.id.mCancel -> {
                    unBindId = item.id.toString()
                    dialog.show()
                }
                R.id.mEditName -> {
//                    bundle.clear()
//                    bundle.putSerializable("bean", item)
//                    RouteManager.goAct(ARouterUrl.BIND_FAMILY, bundle)
                    showRemarkDialog(item)
                }
                R.id.mParent -> {
                    bundle.clear()
                    bundle.putString("id", item.userJointId.toString())
                //    RouteManager.goAct(ARouterUrl.FAMILY_INFO, bundle)
                }
                R.id.mChild -> {
                    bundle.clear()
                    bundle.putSerializable("bean", item)
                    RouteManager.goAct(ARouterUrl.FRIEND_WATCH,bundle)
                //  RouteManager.goAct(ARouterUrl.FAMILY_INFO, bundle)
                }
            }
        }

    }

    private fun showRemarkDialog(data: FamilyBean) {
        remarkDialog = RemarkDialog(this, data.fCall) { remark ->
                mViewModel.changeFamilyNickName(data,remark,data.type)
            }
        remarkDialog?.show()
    }

    var remarkDialog: RemarkDialog? = null

    override fun loadData() {
        mViewModel.getMyFamilyList()
    }

    override fun liveObserver() {
        mViewModel.familyLive.observe(this) {
            setDate(adapter, it.data)
        }
        mViewModel.cancelLive.observe(this) {
            loadData()
            EventBus.getDefault().post(FamilyEvent())
        }
        mViewModel.sortLive.observe(this) {
            //修改成功
            CommonToastUtils.showSucceedToast("修改成功")
            EventBus.getDefault().post(FamilyEvent())
            loadData()
        }
        mViewModel.changeOk.observe(this){
            CommonToastUtils.showSucceedToast("修改成功")
            EventBus.getDefault().post(FamilyEvent())
            loadData()
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun familyChange(event:FamilyEvent){
        mViewModel.getMyFamilyList()
    }
}