package com.magnify.utils.ui.jsoup

import android.os.Bundle
import android.support.v7.widget.GridLayoutManager
import android.text.TextUtils
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.magnify.utils.R
import com.magnify.utils.ui.jsoup.adapter.JsoupImageAdapter
import com.magnify.utils.ui.jsoup.utils.JsoupUtils
import com.yw.fastviewlibrary.dialog.LoadingDialog
import com.yw.fastviewlibrary.fragment.BaseFragment
import com.yw.xrecylerview.XRecyclerView
import kotlinx.android.synthetic.main.fragment_jsoup_main.*
import net.EellyBaseRetrofitApi
import net.listeners.SimpleCallBack
import retrofit2.Call
import retrofit2.Response
import utils.utils.LogUtils

/**
 * @author:heinigger 2019/6/17  9:40
 * @function:
 */
class JsoupImageFragment : BaseFragment() {

    internal var html = ""
    internal var rawHtml = NetBianConfig.URL
    internal var mImageAdapters: JsoupImageAdapter? = null

    private var mPage = 1
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        return LayoutInflater.from(context).inflate(R.layout.fragment_jsoup_main, null)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val type = arguments?.getSerializable(KEY_TYPE) as NetBianConfig.TYPE
        rawHtml += (if (TextUtils.isEmpty(type.type)) "" else "/${type.type}")
        html = rawHtml
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        recyler!!.layoutManager = GridLayoutManager(context, 3)
        recyler!!.setLoadingListener(object : XRecyclerView.LoadingListener {
            override fun onRefresh() {
                html = rawHtml
                lazyLoadDatas()
            }

            override fun onLoadMore() {
                mPage = mPage + 1
                LogUtils.v("JsoupActivity", mPage)
                html = "${rawHtml}/index_$mPage.html"
                lazyLoadDatas()
            }
        })
    }

    override fun lazyLoadDatas() {
        if (mPage == 1) LoadingDialog.showDialog(context)
        EellyBaseRetrofitApi.getBasicService().gethtml(html).enqueue(object : SimpleCallBack<String>() {
            override fun onResponse(call: Call<String>, response: Response<String>) {
                val mUrls = JsoupUtils.getHighDefinitionImgs(NetBianConfig.URL, response.body())
                if (mPage == 1) {
                    recyler!!.refreshComplete()
                } else {
                    recyler!!.loadMoreComplete()
                }
                if (mImageAdapters == null) {
                    mImageAdapters = JsoupImageAdapter(context, mUrls)
                    recyler!!.adapter = mImageAdapters
                } else {
                    mImageAdapters!!.addDatas(mUrls)
                }
                LoadingDialog.dissmissDialog(context)
            }
        })
    }

    companion object {
        private val KEY_TYPE = "TYPE"

        fun newInstant(type: NetBianConfig.TYPE): JsoupImageFragment {
            val bundle = Bundle()
            bundle.putSerializable(KEY_TYPE, type)
            val mFragment = JsoupImageFragment()
            mFragment.arguments = bundle
            return mFragment
        }
    }
}
