package com.example.mysmartlab

import android.content.Context
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.RadioButton
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.mysmartlab.Adapter.AdapterCatalog
import com.example.mysmartlab.Adapter.AdapterCategory
import com.example.mysmartlab.Adapter.AdapterNews
import com.example.mysmartlab.Model.APIUse
import com.example.mysmartlab.Model.APIRequest
import com.example.mysmartlab.Model.ApiHttp
import com.example.mysmartlab.Model.CatalogItem
import com.example.mysmartlab.Model.NewsItem
import com.example.mysmartlab.databinding.ActivityAnaliseBinding
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Runnable
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.awaitResponse
import retrofit2.converter.gson.GsonConverterFactory


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

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


        })

    }
    fun InitListNews(data: ArrayList<NewsItem>){
        with(binding){
            news.layoutManager = LinearLayoutManager(this@ActivityAnalise, LinearLayoutManager.HORIZONTAL, false)
            news.adapter = adapterNews
            val listNews: ArrayList<NewsItem> = data!!
            if(listNews.isNotEmpty()){
                for (element in listNews) {
                    adapterNews.addNew(element)
                }
            }
        }
    }


}
