package com.colorful.keyboard.theme.fragment

import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.colorful.keyboard.theme.ColorfulDetailsActivity
import com.colorful.keyboard.theme.R
import com.colorful.keyboard.theme.config_adpter.PhotoListAdapter
import com.colorful.keyboard.theme.model.ThemePhotoListBean
import com.colorful.keyboard.theme.net.ApiCallbackCommon
import com.colorful.keyboard.theme.net.ApiException
import com.colorful.keyboard.theme.net.BaseInitApiRequest

class KeyboardListFragment : Fragment() {
    private var swipeRefreshLayout: SwipeRefreshLayout? = null
    private var recyclerView: RecyclerView? = null
    private var catid: String? = ""
    private var photoListAdapter: PhotoListAdapter? = null
    private var loadingLayout: LinearLayout? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        if (arguments != null) {
            val arguments = arguments
            catid = arguments!!.getString("id")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val inflate = inflater.inflate(R.layout.fragment_photo_list, container, false)
        swipeRefreshLayout = inflate.findViewById(R.id.swiper)
        recyclerView = inflate.findViewById(R.id.rv_photo_ist)
        loadingLayout = inflate.findViewById(R.id.loadingLayout)
        return inflate
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        loadingLayout?.visibility = View.VISIBLE
        //recyclerView.setAdapter(new PhotoListAdapter());
        swipeRefreshLayout!!.isEnabled = false
        photoListAdapter = PhotoListAdapter()
        recyclerView!!.layoutManager = GridLayoutManager(activity, 3)
        recyclerView!!.adapter = photoListAdapter
        photoListAdapter!!.setOnItemClickListener { baseQuickAdapter, view, i ->
            startActivity(
                Intent(activity, ColorfulDetailsActivity::class.java)
                    .putExtra("title", baseQuickAdapter.getItem(i)!!.title)
                    .putExtra("key", baseQuickAdapter.getItem(i)!!.key).putExtra(
                        "url", baseQuickAdapter.getItem(i)!!
                            .thumbUrl
                    )
                    .putExtra("",baseQuickAdapter.getItem(i)?.thumbUrlGif)
            )
        }
        info
    }

    private val info: Unit
        private get() {
            val hashMap = HashMap<String, String>()
            hashMap["offset"] = "0"
            hashMap["fetch_size"] = "100"
            hashMap["sign"] = "a28aadc61c76c754944f6ddb48962c9c"
            BaseInitApiRequest.getNetData(BaseInitApiRequest.initHHYApiStores()
                .getThemeList(catid, hashMap),
                object : ApiCallbackCommon<ThemePhotoListBean?>(activity, false) {


                    override fun onSuccess(model: ThemePhotoListBean?) {
                        photoListAdapter!!.submitList(model?.data?.sections!![0]?.items)

                        loadingLayout?.visibility = View.GONE
                    }

                    override fun onFailure(ex: ApiException) {
                        loadingLayout?.visibility = View.GONE
                    }
                })
        }
}