package com.bc_chat.circle.fragment

import android.content.Intent
import com.bc_chat.bc_base.entity.contact.Dynamic
import com.bc_chat.circle.activity.TopicDetailsActivity
import com.bc_chat.circle.activity.UserCircleDetailsActivity
import com.bc_chat.circle.adapetr.DynamicAdapter
import com.bc_chat.circle.dataSource.DynamicDataSource
import com.zhaohaoting.framework.abs.BaseRecyclerFragment
import com.zhaohaoting.framework.abs.adapter.AbsRecyclerAdapter
import com.zhaohaoting.framework.imagepicker.ImagePicker
import com.zhaohaoting.framework.imagepicker.bean.MediaItem
import com.zhaohaoting.framework.imagepicker.ui.ImagePreviewActivity
import com.zhaohaoting.framework.mvchelper.mvc.IAsyncDataSource
import com.zhaohaoting.framework.utils.ActivityUtils
import org.jetbrains.anko.bundleOf
import java.util.*

/**
 * author：Created by zhaohaoting on 2019/8/27
 * email：526309416@qq.com
 * desc：
 */
class DynamicFragment : BaseRecyclerFragment<Dynamic, List<Dynamic>>() {

    companion object {
        const val TOPIC_DETAILS = 42

        fun newInstance(type: String): DynamicFragment {
            val fragment = DynamicFragment()
            fragment.arguments = bundleOf("type" to type)
            return fragment
        }
    }

    private val dynamicAdapter by lazy {
        DynamicAdapter(activity)
    }


    var type: String? = "1"

    override fun getAdapter(): AbsRecyclerAdapter<Dynamic, List<Dynamic>> = dynamicAdapter

    override fun getDataSource(): IAsyncDataSource<List<Dynamic>> = DynamicDataSource(type)

    override fun onInit() {
        arguments?.let {
            type = it.getString("type")
        }
        dynamicAdapter.setOnDynamicClickListener(object : DynamicAdapter.OnDynamicClickListener {
            override fun itemPhotoClick(uid: String) {
                ActivityUtils.newInstance(this@DynamicFragment)
                        .putExtra("cuid", uid)
                        .navigation(UserCircleDetailsActivity::class.java)
            }

            override fun itemImageClick(imageList: List<MediaItem>, index: Int, realPosition: Int) {
                if (index == -1) {
                    ActivityUtils.newInstance(this@DynamicFragment)
                            .putExtra("cid", dynamicAdapter.data.get(realPosition).cid)
                            .navigation(TopicDetailsActivity::class.java, TOPIC_DETAILS)
                } else {
                    ActivityUtils.newInstance(this@DynamicFragment)
                            .putParcelableArrayListExtra(ImagePicker.EXTRA_IMAGE_ITEMS, ArrayList(imageList))
                            .putExtra(ImagePicker.EXTRA_SELECTED_IMAGE_POSITION, index)
                            .putExtra(ImagePicker.EXTRA_FROM_ITEMS, true)
                            .navigation(ImagePreviewActivity::class.java)
                }
            }
        })
    }

    fun refresh(isTop: Boolean) {
        if (isTop) {
            recyclerView.smoothScrollToPosition(0)
        }
        super.refresh()
    }

    override fun itemClicked(vh: AbsRecyclerAdapter.BaseViewHolder<*>?, position: Int) {
        ActivityUtils.newInstance(this@DynamicFragment)
                .putExtra("cid", dynamicAdapter.data.get(position).cid)
                .navigation(TopicDetailsActivity::class.java, TOPIC_DETAILS)
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == TOPIC_DETAILS) {
            refresh()
        }
    }
}