package com.example.what_the_fantasy.data.services

import android.annotation.SuppressLint
import android.util.Log
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.CommentStub.comments
import com.example.what_the_fantasy.data.local.QuoteStub
import com.example.what_the_fantasy.data.model.Comment
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) {
        val user = getUserById(index)
        user?.username = username

        //Afficher tous les users
        logsUser.logDebugDisplayUsers(getAllUsers(), "UsernameUpdate")
    }

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

        //Afficher tous les users
        logsUser.logDebugDisplayUsers(getAllUsers(), "EmailUpdate")
    }

    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.logDebugDisplayUsers(getAllUsers(), "PasswordUpdate")
    }

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

    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
        for (user in userStub) {
            if (user.username == username) {
                return false
            }
        }
        val user = User(nbUser+1,username, email, date,randomImage(userStub), passwordhash, SrcLanguage.vo)
        users.add(user)//ajout au stub

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

    override fun getAllUsers(): List<User> = users

    override fun getComment(quoteId: Int): List<Comment> = comments

    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  (QuoteStub.allQuotes.find { it.id == id })
    }

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

    override fun getFavorite(username: String) {
        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"
    }
}