package com.ydl.confide.home.adapter

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.EditorInfo
import android.widget.EditText
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.ydl.confide.R
import com.ydl.confide.home.ConfideHomeActivity
import com.ydl.confide.home.ConfideSearchActivity
import com.ydl.confide.home.bean.ConfideHomeDataBean
import com.ydl.confide.home.config.IConfideHomeConfig
import com.ydl.confide.home.constants.FinalString
import com.ydl.confide.home.constants.FinalString.SEARCH_REQUEST_CODE
import com.ydl.confide.home.contract.IConfideHomeContract
import com.ydl.confide.home.event.IConfideHomeEvent
import com.ydl.confide.home.widget.*
import com.ydl.ydlcommon.utils.DisplayUtils
import com.ydl.ydlcommon.utils.KeyboardChangeListener
import com.yidianling.common.tools.RxKeyboardTool
import kotlinx.android.synthetic.main.confide_home_activity.view.*

/**
 * @author yuanwai
 * @描述:倾诉首页列表适配器（因为section框架有问题，故先改为传统方式编写）
 * @Copyright Copyright (c) 2018
 * @Company 壹点灵
 * @date 2018/12/14
 */
class ConfideHomeAdapter(
    private val mContext: Context,
    private var confideHomeEvent: IConfideHomeEvent,
    private var confideHomeView: IConfideHomeContract.View
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
    private var mListData: List<ConfideHomeDataBean>? = null

    private var mFilterViewHolder: FilterViewHolder? = null

    fun getFilterViewHolder(): FilterViewHolder? {
        return mFilterViewHolder
    }

    init {
        if (null == mListData) {
            mListData = ArrayList()
        }
    }

    override fun getItemViewType(position: Int): Int {
        if (null == mListData || mListData!!.isEmpty() || position >= mListData!!.size) {
            //当没数据 返回一个不存在的类型
            return 0
        }
        return mListData!![position].type!!
    }

    fun updateItems(list: List<ConfideHomeDataBean>) {
        if (null == mListData || mListData!!.isEmpty()) {
            mListData = ArrayList()
        } else {
            (mListData as ArrayList).clear()
        }
        (mListData as ArrayList).addAll(list)
    }

    fun addItems(list: List<ConfideHomeDataBean>) {
        if (null == mListData || mListData!!.isEmpty()) {
            mListData = ArrayList()
        }
        (mListData as ArrayList).addAll(list)
    }

    fun getList(): List<ConfideHomeDataBean>? {
        return mListData
    }

    fun getCount(): Int {
        if (null == mListData || mListData!!.isEmpty()) {
            return 0
        }
        return mListData!!.size
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        when (viewType) {
            //banner
//            IConfideHomeConfig.TYPE_BANNER -> {
//                return BannerViewHolder(ConfideHomeBannerView(parent.context, confideHomeEvent))
//            }
            //最近倾诉
            IConfideHomeConfig.TYPE_RECENTLY_CONFIDED -> {
//                return ConsultViewHolder(ConfideHomeRecentView(parent.context, confideHomeEvent))
                val recyclerView = RecyclerView(parent.context)
                recyclerView.layoutParams = ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                ).apply {
                    leftMargin = DisplayUtils.dip2px(parent.context, 4F)
                    rightMargin = DisplayUtils.dip2px(parent.context, 4F)
                }
                return RecentConfideHolder(recyclerView)
            }
            //分类
//            IConfideHomeConfig.TYPE_CATEGORY -> {
//                return CategoryViewHolder(ConfideHomeCategoryView(parent.context, confideHomeEvent, confideHomeView))
//            }
            //筛选模块
            IConfideHomeConfig.TYPE_RECOMMEND_FILTER -> {
                return FilterViewHolder(ConfideHomeFilterView(parent.context, confideHomeView))
            }
            //为你推荐
            IConfideHomeConfig.TYPE_RECOMMEND -> {
                return RecommendViewHolder(
                    ConfideHomeRecommendView(
                        confideHomeView,
                        parent.context,
                        confideHomeEvent
                    )
                )
            }
            IConfideHomeConfig.TYPE_VIDEO_SHOW -> {
                val recyclerView = RecyclerView(parent.context)
                recyclerView.layoutParams = ViewGroup.MarginLayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT
                ).apply {
                    leftMargin = DisplayUtils.dip2px(parent.context, 6F)
                    rightMargin = DisplayUtils.dip2px(parent.context, 6F)
                }
                return VideoShowHolder(recyclerView)
            }

            //顶部搜索
            IConfideHomeConfig.TYPE_SEARCH -> {
                return SearchViewHolder(
                    LayoutInflater.from(mContext)
                        .inflate(R.layout.confide_home_search_header, parent, false)
                )
            }

            //空白页
            IConfideHomeConfig.TYPE_EMPTY -> {
                return EmptyView(LayoutInflater.from(mContext).inflate(R.layout.confide_empty_layout, parent,false))
            }

//            //听声寻人
//            IConfideHomeConfig.TYPE_SOUND -> {
////                return ConfideHomeListenAndFoundViewHolder(ConfideHomeListenAndFoundView(parent.context, confideHomeEvent))
//            }
            else -> {
                return EmptyViewHolder(View.inflate(parent.context, R.layout.confide_empty, null))
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) =
        /*if (holder is BannerViewHolder && null != holder.bannerView) {
            //banner
            holder.bannerView!!.initData(mListData!![position])
        } else if (holder is CategoryViewHolder && null != holder.categoryView) {
            //分类
            holder.categoryView!!.initData(mListData!![position])
        } else */if (holder is FilterViewHolder && null != holder.filterView) {
        mFilterViewHolder = holder
        //筛选模块
        holder.filterView.initData(mListData!![position])
    } else if (holder is ConfideHomeListenAndFoundViewHolder) {
        //听声寻人
//                holder.confideHomeListenAndFoundView!!.initData(mListData!![position])
    } else if (holder is RecentConfideHolder) {
        //最近倾诉
        holder.rv.layoutManager =
            LinearLayoutManager(holder.itemView.context, RecyclerView.HORIZONTAL, false)
        val recentBody = mListData!![position].body
        val data = recentBody?.map { ItemVideoShowViewModel().mapOf(it) } ?: emptyList()
        holder.rv.adapter = RecentConfideAdapter(data, confideHomeEvent)
//                holder.consultView!!.initData(mListData!![position])
    } else if (holder is RecommendViewHolder && null != mListData!![position] && null != mListData!![position].body && !mListData!![position].body!!.isEmpty()) {
        //为你推荐
        holder.confideHomeRecommendView.initData(
            mListData!![position].body!![0],
            position,
            mListData!![position].recommendId
        )
    } else if (holder is VideoShowHolder) {
        holder.rv.layoutManager =
            LinearLayoutManager(holder.itemView.context, RecyclerView.HORIZONTAL, false)
        val recentBody = mListData!![position].body
        holder.rv.adapter = VideoShowAdapter(recentBody, confideHomeEvent)
    } else if (holder is SearchViewHolder) {
        holder.searchView.etSearch.setText(mListData!![position].keyword)
        holder.searchView.setOnClickListener {
            var intent = Intent(mContext, ConfideSearchActivity::class.java)
            if (!TextUtils.isEmpty(mListData!![position].keyword)){
                intent.putExtra(FinalString.CONFIDE_SEARCH_KEY_WORD,mListData!![position].keyword)
            }
            if (mContext is ConfideHomeActivity){
                val confideHomeActivity = mContext as ConfideHomeActivity
                confideHomeActivity.startActivityForResult(intent,SEARCH_REQUEST_CODE)
            }
        }
    } else if (holder is EmptyViewHolder) {

    } else {

    }

    override fun getItemCount(): Int {
        return mListData!!.size
    }

    /**
     * banner viewHolder
     */
    inner class BannerViewHolder(itemView: ConfideHomeBannerView) :
        RecyclerView.ViewHolder(itemView) {
        var bannerView: ConfideHomeBannerView? = null

        init {
            bannerView = itemView
        }
    }

    /**
     * 分类测评 viewHolder
     */
    inner class CategoryViewHolder(itemView: ConfideHomeCategoryView) :
        RecyclerView.ViewHolder(itemView) {
        var categoryView: ConfideHomeCategoryView? = null

        init {
            categoryView = itemView
        }
    }

    /**
     * 筛选模块 viewHolder
     */
    inner class FilterViewHolder(var filterView: ConfideHomeFilterView) :
        RecyclerView.ViewHolder(filterView)

    /**
     * 搜索模块 viewHolder
     */
    inner class SearchViewHolder(var searchView: View) : RecyclerView.ViewHolder(searchView)


    inner class EmptyView(var emptyView: View): RecyclerView.ViewHolder(emptyView)

    /**
     * 听声寻人 viewHolder
     */
    inner class ConfideHomeListenAndFoundViewHolder(var confideHomeListenAndFoundView: ConfideHomeListenAndFoundView) :
        RecyclerView.ViewHolder(confideHomeListenAndFoundView)

    /**
     * 最近倾诉 viewHolder
     */
    inner class ConsultViewHolder(var consultView: ConfideHomeRecentView) :
        RecyclerView.ViewHolder(consultView)

    /**
     * 为你推荐 viewHolder
     */
    inner class RecommendViewHolder(var confideHomeRecommendView: ConfideHomeRecommendView) :
        RecyclerView.ViewHolder(confideHomeRecommendView)

    /**
     * 空ViewHolder
     */
    inner class EmptyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        var view: View? = null

        init {
            view = itemView
        }
    }

    class VideoShowHolder(val rv: RecyclerView) : RecyclerView.ViewHolder(rv)
    class RecentConfideHolder(val rv: RecyclerView) : RecyclerView.ViewHolder(rv)
}