package com.zswc.creativity.fragment

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.ViewGroup
import com.hwangjr.rxbus.annotation.Subscribe
import com.hwangjr.rxbus.annotation.Tag
import com.qmuiteam.qmui.kotlin.onClick
import com.tencent.mmkv.MMKV
import com.ysnows.base.utils.UiUtils
import com.zswc.creativity.R
import com.zswc.creativity.adapter.CollectAdapter
import com.zswc.creativity.base.MBFragment
import com.zswc.creativity.config.ConfigRx
import com.zswc.creativity.databinding.FragmentHomeMyBinding
import com.zswc.creativity.model.MyCollection
import com.zswc.creativity.utils.GridSpaceItemToDecoration
import com.zswc.creativity.vmodel.HomeMyVModel


/**
 * 我的
 *Created by shihuiyun
 *on 2020/9/11
 */
class HomeMyFragment : MBFragment<HomeMyVModel, FragmentHomeMyBinding>() {
//    var fragments = emptyArray<Fragment>()

    var adapter: CollectAdapter? = null
    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

        //获取登录状态
        var token = MMKV.defaultMMKV().decodeString("token", "")
        if (token.equals("")) {
            vm.isLogin.value = false
        }
        vm.getUserInfo()

//        binding.recyclerCollectView.addItemDecoration(GridSpaceItemToDecoration(2,
//                UiUtils.dp2Px(context(), 10f),
//                UiUtils.dp2Px(context(), 15f)))

        adapter = CollectAdapter()
        binding.recyclerCollectView.adapter = adapter

        vm.getMyCollection("")

        vm.list.observe(this) {
            //刷新
            vm.is_show.value = it.isEmpty()
            adapter?.addData(it)
        }
    }

    override fun listeners() {
        super.listeners()
        adapter?.setOnItemClickListener { adapter, view, position ->

//            vm.list.value?.get(position)?.id
//
//            UiSwitch.bundle(context, TibetanDetailsActivity::class.java,
//                    B().putString("id", vm.list.value?.get(position)?.id)
//                            .ok())
        }
        binding.tvAll.onClick {
            adapter?.clearList()
            vm.list.value = ArrayList<MyCollection>()
            binding.tvAll.setBackgroundResource(R.drawable.bg_chose_gray)
            binding.tvOne.setBackgroundResource(R.drawable.bg_chose_white)
            binding.tvTwo.setBackgroundResource(R.drawable.bg_chose_white)
            vm.getMyCollection("")
        }
        binding.tvOne.onClick {
            adapter?.clearList()
            vm.list.value = ArrayList<MyCollection>()
            binding.tvAll.setBackgroundResource(R.drawable.bg_chose_white)
            binding.tvOne.setBackgroundResource(R.drawable.bg_chose_gray)
            binding.tvTwo.setBackgroundResource(R.drawable.bg_chose_white)
            vm.getMyCollection("1")

        }
        binding.tvTwo.onClick {
            adapter?.clearList()
            vm.list.value = ArrayList<MyCollection>()
            binding.tvAll.setBackgroundResource(R.drawable.bg_chose_white)
            binding.tvOne.setBackgroundResource(R.drawable.bg_chose_white)
            binding.tvTwo.setBackgroundResource(R.drawable.bg_chose_gray)
            vm.getMyCollection("3")
        }


    }


    override fun vmClass(): Class<HomeMyVModel> {
        return HomeMyVModel::class.java
    }

    override fun binding(inflater: LayoutInflater, container: ViewGroup?): FragmentHomeMyBinding {
        return FragmentHomeMyBinding.inflate(layoutInflater)
    }

    override fun title(): String? {
        return null
    }

    override val isRxbus: Boolean
        get() = true

    @Subscribe(tags = [Tag(ConfigRx.REFRESH_USER)])
    fun setRefreshUser(type: String) {
        vm.getUserInfo()
    }
}
