package com.example.smartlab

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import android.os.Handler
import android.widget.Toast
import com.example.smartlab.Api.AdapterCatalog
import com.example.smartlab.Api.AdapterNews
import com.example.smartlab.Api.ApiHttp
import com.example.smartlab.Fragments.ItemInformationDialogFragment
import com.example.smartlab.Models.Catalog
import com.example.smartlab.Models.News
import com.example.smartlab.databinding.AnalizesMainActivityBinding
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.util.*
import kotlin.collections.ArrayList

class AnalizesActivity : AppCompatActivity(), AdapterCatalog.Listener {
    lateinit var binding: AnalizesMainActivityBinding
    private lateinit var handler: Handler
    private lateinit var runnable: Runnable
    private val adapterNews = AdapterNews()
    private val adapterCatalog = AdapterCatalog(this)
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = AnalizesMainActivityBinding.inflate(layoutInflater)
        setContentView(binding.root)
        GetData()
        handler = Handler()
        binding.newsFeedSwiperefresh.setOnClickListener{
            runnable = Runnable {
                GetData()
                binding.newsFeedSwiperefresh.isRefreshing = false
            }
            handler.postDelayed(
                runnable, 1000.toLong()
            )
        }
    }
    private fun GetNews(){
        var arListNews = ApiHttp.retrifitApi.getNews().enqueue(object: Callback<ArrayList<News>>{
            override fun onResponse(
                call: Call<ArrayList<News>>,
                response: Response<ArrayList<News>>
            ) {
                if (response.code() == 200) {
                    val data = response.body()
                    runOnUiThread { InitListNews(data!!) }
                }
                else{
                    Toast.makeText(this@AnalizesActivity, "Новостей нету", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ArrayList<News>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })

    }
    private fun GetCatalog(){
        var arListCatalog = ApiHttp.retrifitApi.getCatalog().enqueue(object: Callback<ArrayList<Catalog>>{
            override fun onResponse(
                call: Call<ArrayList<Catalog>>,
                response: Response<ArrayList<Catalog>>
            ) {
                if(response.code()==200){
                    val data = response.body()
                    runOnUiThread { InitListCatalog(data!!) }
                }
                else{
                    Toast.makeText(this@AnalizesActivity, "Каталога нету", Toast.LENGTH_SHORT).show()
                }
            }

            override fun onFailure(call: Call<ArrayList<Catalog>>, t: Throwable) {
                TODO("Not yet implemented")
            }

        })
    }

    fun InitListNews(data: ArrayList<News>){
        with(binding){
            news.layoutManager = LinearLayoutManager(this@AnalizesActivity, LinearLayoutManager.HORIZONTAL, false)
            news.adapter = adapterNews
        val listNews: ArrayList<News> = data!!
            if(listNews.isNotEmpty()){
                for (element in listNews) {
                    adapterNews.addNews(element)
                }
            }
        }
    }
    fun InitListCatalog(data: ArrayList<Catalog>){
        with(binding){
            catalog.layoutManager = LinearLayoutManager(this@AnalizesActivity, LinearLayoutManager.VERTICAL, false)
            catalog.adapter = adapterCatalog
            val listCatalog: ArrayList<Catalog> = data!!
            if(listCatalog.isNotEmpty()){
                for(element in listCatalog){
                    adapterCatalog.addCatalog(element)
                }
            }
        }
    }
    fun GetData(){
        GetNews()
        GetCatalog()
    }

    override fun OnClick(catalog: Catalog) {
       val itemInformationDialogFragment = ItemInformationDialogFragment()
       val bundle = Bundle()
        bundle.putSerializable(
            "iteminformation",
            catalog
        )
       itemInformationDialogFragment.arguments = bundle
        itemInformationDialogFragment.show(supportFragmentManager, "pop")
    }
}



