//
//  GameEngine.swift
//  Pendu
//
//  Created by Sofiane Lasri-Trienpont on 09/05/2023.
//

import UIKit
import CoreData

class GameEngine: NSObject {

    private var listThemeAnimalsEasy : [String] = ["Chien", "Chat", "Lion", "Girafe", "Éléphant", "Serpent", "Oiseau", "Tigre", "Papillon", "Singe"];
    private var listThemeSportsEasy : [String] = ["Football", "Basket-ball", "Tennis", "Natation", "Rugby", "Golf", "Volley-ball", "Baseball", "Athlétisme", "Boxe"];
    private var listThemeFoodEasy : [String] = ["Pomme", "Banane", "Carotte", "Poisson", "Riz", "Fromage", "Pain", "Chocolat", "Fraise", "Poulet"];
    private var listThemeCitiesEasy : [String] = ["Paris", "Londres", "New York", "Rome", "Tokyo", "Sydney", "Barcelone", "Berlin", "Istanbul", "Mumbai"];
    private var listThemeColorsEasy : [String] = ["Rouge", "Bleu", "Vert", "Jaune", "Rose", "Violet", "Orange", "Gris", "Marron", "Noir"];

    private var listThemeAnimalsHard : [String] = ["Chimpanzé", "Lynx", "Hippopotame", "Pélican", "Kangourou", "Anaconda", "Chimère", "Panda roux", "Calmar géant", "Ouistiti"];
    private var listThemeSportsHard : [String] = ["Escrime", "Squash", "Haltérophilie", "Badminton", "Ski de fond", "Pentathlon moderne", "Canoë-kayak", "Aviron", "Patinage artistique", "Triathlon"];
    private var listThemeFoodHard : [String] = ["Artichaut", "Quinoa", "Courge", "Wasabi", "Chou-fleur", "Sarrasin", "Salsifis", "Morue", "Canneberge", "Pistache"];
    private var listThemeCitiesHard : [String] = ["Copenhague", "Budapest", "Dubrovnik", "Séoul", "Marrakech", "Reykjavik", "La Nouvelle-Orléans", "Bratislava", "Wellington", "Accra"];
    private var listThemeColorsHard : [String] = ["Écarlate", "Azur", "Émeraude", "Safran", "Pourpre", "Indigo", "Sienne", "Cyan", "Vermillon", "Lavande"];
    
    private var wordToGuess : String = "";
    private var attemptsRemaining : Int = 0;
    private var guessedLetters : [Character] = [];
    private var theme : String = "";
    private var difficulty : String = "";
    private var playerName : String = "";
    private var score : Int = 0;
    private var combo : Int = 0;
    
    private var managedObjectContext: NSManagedObjectContext?;
 
    override init() {
        //
    }
    
    func WordToGuess(theme: String, difficulty: String) -> String{
        var word = ""
        self.theme = theme;
        self.difficulty = difficulty;
        if theme == "Animals"{
            if difficulty == "Easy"{
                let randomInt = Int.random(in: 0..<listThemeAnimalsEasy.count)
                word = listThemeAnimalsEasy[randomInt]
            } else {
                let randomInt = Int.random(in: 0..<listThemeAnimalsHard.count)
                word = listThemeAnimalsHard[randomInt]
            }
        } else if theme == "Sports"{
            if difficulty == "Easy"{
                let randomInt = Int.random(in: 0..<listThemeSportsEasy.count)
                word = listThemeAnimalsEasy[randomInt]
            } else {
                let randomInt = Int.random(in: 0..<listThemeSportsHard.count)
                word = listThemeSportsHard[randomInt]
            }
        } else if theme == "Food"{
            if difficulty == "Easy"{
                let randomInt = Int.random(in: 0..<listThemeFoodEasy.count)
                word = listThemeFoodEasy[randomInt]
            } else {
                let randomInt = Int.random(in: 0..<listThemeFoodHard.count)
                word = listThemeFoodHard[randomInt]
            }
        } else if theme == "Cities"{
            if difficulty == "Easy"{
                let randomInt = Int.random(in: 0..<listThemeCitiesEasy.count)
                word = listThemeCitiesEasy[randomInt]
            } else {
                let randomInt = Int.random(in: 0..<listThemeCitiesHard.count)
                word = listThemeCitiesHard[randomInt]
            }
        } else if theme == "Colors"{
            if difficulty == "Easy"{
                let randomInt = Int.random(in: 0..<listThemeColorsEasy.count)
                word = listThemeColorsEasy[randomInt]
            } else {
                let randomInt = Int.random(in: 0..<listThemeColorsHard.count)
                word = listThemeColorsHard[randomInt]
            }
        }
        return word
    }
    
    //public func startNewGame(difficulty: String) -> Void {
        //wordToGuess = WordToGuess(theme: "Animals", difficulty: "Easy")
    //}
    
    public func guessLetter(letter: Character) -> Bool {

        if wordToGuess.contains(letter) {
            return true
        } else {
            return false
        }

    }
    
    public func isGameOver() -> Bool {
        // Vérification si le jeu est terminé
        return false;
    }
    
    public func isLetterGuesses(letter: Character) -> Bool {
        return false;
    }
    
    public func saveScore() -> Void {
        // Enregistrement du score dans la base Core Data
        
        // Création du gestionnaire d'objets persistants
        let context: NSManagedObjectContext = (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext;
        
        // Création de l'instance de l'entité CoreData
        let entity = NSEntityDescription.entity(forEntityName: "Score", in: context)!;
        let object = NSManagedObject(entity: entity, insertInto: context);
        object.setValue(theme, forKeyPath: "categoryName");
        object.setValue(difficulty, forKeyPath: "difficulty");
        object.setValue(playerName, forKeyPath: "playerName");
        object.setValue(score, forKeyPath: "score");
        
        // On enregistre
        do {
            try context.save();
        } catch let error as NSError {
            print("Erreur lors de l'enregistrement des données : \(error), \(error.userInfo)");
        }
    }
    
    static func getOnlineScoreboard() -> Void {
        // Récupération du classement depuis le serveur
    }
}