package com.dreamteam.timelapse

import android.content.DialogInterface
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.EditText
import android.widget.LinearLayout
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.ui.AppBarConfiguration
import androidx.viewpager2.widget.ViewPager2
import com.dreamteam.timelapse.databinding.ActivityMainBinding
import com.google.android.material.snackbar.Snackbar
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.dreamteam.timelapse.R as Rtmp
import com.dreamteam.timelapse.R
import com.dreamteam.timelapse.data.ApiService
import com.dreamteam.timelapse.data.ProjectRepository
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory


class MainActivity : AppCompatActivity() {

    private lateinit var appBarConfiguration: AppBarConfiguration
    private lateinit var binding: ActivityMainBinding
    private lateinit var dialog: AlertDialog
    private lateinit var projectRepository : ProjectRepository

    override fun onCreate(savedInstanceState: Bundle?) {
//        super.onCreate(savedInstanceState)
//
        val retrofit = Retrofit.Builder()
            .baseUrl("https://timelapse.kerboul.me/api/")
            .addConverterFactory(GsonConverterFactory.create())
            .build()
        projectRepository = ProjectRepository(retrofit.create(ApiService::class.java))
        Log.d("mainActivity", "La main activity est créée")


        super.onCreate(savedInstanceState)
        //val navController = findNavController(R.id.nav_host_fragment_content_main)
        //appBarConfiguration = AppBarConfiguration(navController.graph)
        //setupActionBarWithNavController(navController, appBarConfiguration)

        // R.layout contient :
        // - un TabLayout avec l'id tabLayout
        // - un ViewPager2 avec l'id viewPager
        // - un FloatingActionButton avec l'id fab

        //setContentView(R.layout.activity_main)
        //ce qui corrigé donne :
        setContentView(Rtmp.layout.activity_main)
        val tabLayout = findViewById<View>(Rtmp.id.tabLayout) as TabLayout
        val viewPager = findViewById<View>(Rtmp.id.viewPager) as ViewPager2
        createDialog(findViewById<View>(Rtmp.id.fab))


        // Configurer l'adapter pour ViewPager2
        var tabsAdapter = TabsAdapter(this)
        viewPager.setAdapter(tabsAdapter)

        // Connecter le ViewPager au TabLayout
        TabLayoutMediator(
            tabLayout, viewPager
        ) { tab: TabLayout.Tab, position: Int ->
            when (position) {
                0 -> tab.setText("Mes projets") // Nom du premier onglet
                1 -> tab.setText("Mes vidéos") // Nom du second onglet
            }
        }.attach()

        //Bouton flottant
        findViewById<View>(Rtmp.id.fab).setOnClickListener { view -> run {
            this.dialog.show()
            }
        }
    }

    fun createDialog(viewfortoast: View){
        //Preparing views
        val inflater = getSystemService(LAYOUT_INFLATER_SERVICE) as LayoutInflater
        val layout: View = inflater.inflate(
            R.layout.project_create_dialog_layout,
            findViewById<LinearLayout>(R.id.layout_root)
        )

        val project_name = layout.findViewById<View>(Rtmp.id.project_name_dialog) as EditText
        val project_desc = layout.findViewById<View>(Rtmp.id.project_desc_dialog) as EditText

        val builder: AlertDialog.Builder = AlertDialog.Builder(this)
        builder.setView(layout)
        builder.setPositiveButton("Sauvegarder",
            DialogInterface.OnClickListener { dialog, which ->
                val onSuccess : () -> Unit = {
                        dialog.dismiss() ;
                        Snackbar.make(viewfortoast, "Nouveau projet créé", Snackbar.LENGTH_LONG).setAnchorView(Rtmp.id.fab).show() ;
                }
                val onError : (s:String) -> Unit = { s-> Snackbar.make(viewfortoast, "Erreur lors de la création du projet. ${s}", Snackbar.LENGTH_LONG).setAnchorView(Rtmp.id.fab).show() }

                val name = project_name.text.toString()
                val desc = project_desc.text.toString()
                if (name.isBlank() || desc.isBlank() || name == "" || desc == "") {
                    Snackbar.make(viewfortoast, "Les champs ne doivent pas être vides", Snackbar.LENGTH_LONG)
                        .show()
                }else {
                    projectRepository.createProject(
                        project_name.text.toString(),
                        project_desc.text.toString(),
                        onSuccess,
                        onError
                    )
                }
            })
        builder.setNegativeButton("Cancel",
            DialogInterface.OnClickListener { dialog, which -> dialog.dismiss() })
        val dialog: AlertDialog = builder.create()
        this.dialog = dialog

    }

//    override fun onSupportNavigateUp(): Boolean {
//        val navController = findNavController(R.id.nav_host_fragment_content_main)
//        return navController.navigateUp(appBarConfiguration)
//                || super.onSupportNavigateUp()
//    }
}