package library.houhan.com.listofbooks

import android.animation.ObjectAnimator
import android.content.Context
import android.support.design.widget.FloatingActionButton
import android.support.design.widget.Snackbar
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.DefaultItemAnimator
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.view.GestureDetector
import android.view.LayoutInflater
import android.view.MotionEvent
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.Spinner
import bean.ListOfBooksBean
import com.alibaba.android.arouter.facade.annotation.Route
import constant.Constant
import library.houhan.com.base.BaseAdapter
import library.houhan.com.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_list_of_books.*
import util.EndLessOnScrollListener
import util.LogUtil


/**
 * 作用 书籍列表的Fragment
 * Created by 司马林 on 2017/10/13.
 */
@Route(path = Constant.LIST_OF_BOOKS)
class ListOfBooksFragment : BaseFragment<ListOfBooksView, ListOfBooksPresenter>(), ListOfBooksView {

    var status: Int = 0//筛选条件 0：全部   1：可借   2：不可借
    val pageSize: Int = 10//列表数据尺寸
    var pageIndex: Int = 1//列表页数

    val linearLayoutManager: LinearLayoutManager = LinearLayoutManager(activity)

    lateinit var adapter: BaseAdapter<ListOfBooksBean.Content>
    val rv: RecyclerView by lazy { list_of_books_rv }
    val refresh: SwipeRefreshLayout by lazy { books_refresh }
    val top: FloatingActionButton by lazy { move_top }

    val presenter: ListOfBooksPresenterImpl = ListOfBooksPresenterImpl(ListOfBooksModelImpl())
    override fun showSnack(msg: String) {
        Snackbar.make(coordinator, msg, Snackbar.LENGTH_SHORT).show()
    }

    override fun getLayoutId(): Int = R.layout.fragment_list_of_books

    override fun initPresenter(): ListOfBooksPresenter = presenter

    override fun initView() {

        refresh.setOnRefreshListener {
            pageIndex = 1
            presenter.refresh(pageSize, pageIndex, status)
        }

        rv.addOnScrollListener(object : EndLessOnScrollListener(linearLayoutManager) {
            override fun onSlideUp() {
                top.visibility = View.VISIBLE
            }

            override fun onSlideDown() {
                top.visibility = View.GONE
            }

            override fun isFirst(isFirst: Boolean) {
                if (isFirst) {
                    top.visibility = View.GONE
                }
            }

            override fun onLoadMore(pageIndex: Int) {
                LogUtil.i("pageIndex = " + pageIndex)
                presenter.loadMore(pageSize, pageIndex, status)
            }
        })

    }

    override fun initData() {
        presenter.refresh(pageSize, pageIndex, status)

        //置顶
        top.setOnClickListener {
            rv.smoothScrollToPosition(0)
        }

    }

    override fun setRvAdapter(mAdapter: BaseAdapter<ListOfBooksBean.Content>) {
        adapter = mAdapter
        rv.layoutManager = linearLayoutManager
        rv.itemAnimator = DefaultItemAnimator()
        rv.adapter = adapter

        val headerView: View = LayoutInflater.from(activity).inflate(R.layout.list_of_books_header, rv, false)
        adapter.setHeaderView(headerView)
        val spinner: Spinner = headerView.findViewById(R.id.spinner)
        setSpinner(spinner)

    }

    fun setSpinner(spinner: Spinner) {
        val spinnerAdapter: ArrayAdapter<String> = ArrayAdapter(activity, R.layout.spinner_item, arrayOf("全部", "闲置", "已借出"))
        spinner.adapter = spinnerAdapter

        spinner.onItemSelectedListener = object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(adapterView: AdapterView<*>, view: View, i: Int, l: Long) {
                status = i
                presenter.refresh(pageSize, pageIndex, status)
            }

            override fun onNothingSelected(adapterView: AdapterView<*>) {

            }
        }
    }

    override fun getListOfBooksActivity(): Context = activity

    override fun adapterNotifyDataSetChanged() {
        adapter.notifyDataSetChanged()
    }

    override fun adapterRefresh() {
        adapter.notifyDataSetChanged()
        refresh.isRefreshing = false
    }

}