package com.example.what_the_fantasy.data.services

import android.annotation.SuppressLint
import com.example.what_the_fantasy.data.local.UserStub.users
import com.example.what_the_fantasy.data.model.User
import com.example.what_the_fantasy.Logs.LogsUsers
import com.example.what_the_fantasy.data.local.FavoriteStub.favorites
import com.example.what_the_fantasy.data.local.QuoteStub.quotes
import com.example.what_the_fantasy.data.model.Favorite
import com.example.what_the_fantasy.data.model.Quote
import com.example.what_the_fantasy.data.model.SrcLanguage
import com.example.what_the_fantasy.ui.components.hashPassword
import java.time.LocalDate

class ServicesStub : IServices {
    val logsUser = LogsUsers() //gestion des logs pour les utilisateurs

    override fun EditUsername(username: String, index : Int) : Boolean{
        val user = getUserById(index)

        if(!isUsernameExist(username)){
            user?.username = username
            return true
        }

        //Afficher tous les users
        logsUser.logDebugUserEmail(user, "UsernameUpdate")
        return false
    }

    override fun EditEmail(email: String,index : Int) : Boolean {
        val user = getUserById(index)

        if(!isEmailExist(email)){
            user?.email = email
            return true
        }
        //Afficher tous les users
        logsUser.logDebugAllUsers(getAllUsers(), "EmailUpdate")
        return false
    }

    override fun EditPasswd(passwd: String,index : Int) {
        val user = getUserById(index)
        val passwordhash = hashPassword(passwd)
        user?.password = passwordhash

        //Afficher tous les users en log
        logsUser.logDebugAllUsers(getAllUsers(), "PasswordUpdate")
    }

    override fun EditImage(imageURL: String,index : Int) {
        TODO("Not yet implemented")
    }

    override fun ChangeLangage(user: User) {
        if(user.langage == SrcLanguage.vo){
            user.langage = SrcLanguage.vf
        }
        else{
            user.langage = SrcLanguage.vo
        }
        logsUser.logDebugUserLangage(user, "ChangeLangue")
    }

    override fun CreateUser(username: String, email: String, passwd: String, services : IServices) : Boolean {
        val date =dateDuJour()
        val passwordhash = hashPassword(passwd)

        val userStub = services.getAllUsers()
        val nbUser = userStub.size

        if(!isUsernameExist(username) && !isEmailExist(email)){
            val user = User(nbUser+1,username, email, date,randomImage(userStub), passwordhash, SrcLanguage.vo)
            users.add(user)//ajout au stub

            //Afficher tous les users
            logsUser.logDebugAllUsers(users, "CreateUser")
            return true
        }
        return false
    }

    override fun getFavorite(user: User): List<Quote> {
        val favorite = favorites
        return favorite[0].quote
        //return emptyList()
    }

    override fun getAllFavorite(): List<Favorite> = favorites
    override fun getAllQuote(): List<Quote> = quotes
    override fun getAllUsers(): List<User> = users

    override fun getUserById(id: Int): User? {
        return (users.find { it.id == id+1 })
    }

    override fun SearchQuote(quote: String) {
        TODO("Not yet implemented")
    }

    override fun getQuote(id: Int): Quote? {
        return (quotes.find { it.id == id+1 })
    }

    override fun getSomeQuotes(nb: Int, page: Int): MutableList<Quote> {
        val fromIndex = (page - 1) * nb
        val toIndex = minOf(page * nb, quotes.size)

        if (fromIndex >= quotes.size) return mutableListOf()

        return quotes.subList(fromIndex, toIndex).toMutableList()
    }

    override fun isFavorite(id: Int): Boolean {
        TODO("Not yet implemented")
    }




    //------------------------------------------------------
    @SuppressLint("NewApi")
    fun dateDuJour(): String {
        val date = LocalDate.now()
        return date.toString()
    }

    fun randomImage(usersImage : List<User>) : String{
        return "https://img.freepik.com/vecteurs-libre/personnage-guerrier-homme-fantaisie_1045-194.jpg?size=338&ext=jpg"
    }

    fun isUsernameExist(username : String) : Boolean{
        val userStub = getAllUsers()
        for (user in userStub) {
            if (user.username == username) {
                return true
            }
        }
        return false
    }

    fun isEmailExist(email : String) : Boolean{
        val userStub = getAllUsers()
        for (user in userStub) {
            if (user.email == email) {
                return true
            }
        }
        return false
    }
}