//
//  CommonValorant-ViewModel.swift
//  TipsForLol
//
//  Created by Mehmet Ateş on 8.06.2022.
//

import Foundation

@MainActor class CommonValorantViewModel: ObservableObject{
    // MARK: Home view properties
    @Published var contentType: Int = 0
    
    
    // MARK: All agents view properties
    @Published var agents =  [Agent]()
    @Published var agentsByCatagory =  [String: [Agent]]()
    @Published var agentsByAlhabet = [String: [Agent]]()
    @Published var sortType: Int = 0
    @Published var searchKey: String = ""
    
    // MARK: All weapons view properties
    @Published var weapons = [Weapon]()
    
    // MARK: Common properties
    private var service = ValoGetServices()
    
    init(){
        self.handleAgents()
        self.handleWeapons()
    }
    
    // MARK: Agents view funcs
    func handleAgents(){
        Task{ @MainActor in
            self.service.getAllValoAgents { getRes in
                switch getRes{
                case .failure(_):
                    print("Hata alındı")
                case .success(let getAgents):
                    guard let getAgents = getAgents else { return }
                    DispatchQueue.main.async {
                        for agent in getAgents {
                            self.setAgentByAlfabet(agent: agent)
                            self.setAgentByCatagory(agent: agent)
                        }
                        self.agents = getAgents.sorted(by: { Agent1, Agent2 in
                            Agent1.displayName ?? "" < Agent2.displayName ?? ""
                        })
                    }
                }
            }
        }
    }
    
    func setAgentByCatagory(agent: Agent){
        let agentCat = (agent.role?.displayName ?? "").capitalized
        if self.agentsByCatagory[agentCat] == nil{
            self.agentsByCatagory[agentCat] = []
            self.agentsByCatagory[agentCat]!.append(agent)
        }else{
            self.agentsByCatagory[agentCat]!.append(agent)
        }
    }
    
    func setAgentByAlfabet(agent: Agent){
        let firstChar = String(agent.displayName?.prefix(1) ?? "")
        
        var array = self.agentsByAlhabet[firstChar] ?? []
        array.append(agent)
        sortList(firstChar)
        
        self.agentsByAlhabet[firstChar] = array
        sortList(firstChar)
    }
    
    func sortList(_ prefix: String? = ""){
        if let prefix = prefix {
            self.agentsByAlhabet[prefix]?.sort(by: { a1, a2 in
                (a1.displayName ?? "") < (a2.displayName ?? "")
            })
        }
    }
    
    func getTitleList() -> [String]{
        if sortType == 0{
            return agentsByCatagory.keys.sorted()
        }else{
            return agentsByAlhabet.keys.sorted()
        }
    }
    
    func getSubList(id: String) -> [Agent]{
        if sortType == 0{
            return agentsByCatagory[id] ?? []
        }else{
            return agentsByAlhabet[id] ?? []
        }
    }
    
    // MARK: Weapons view funcs
    func handleWeapons(){
        Task{ @MainActor in
            self.service.getAllValoWeapons { getRes in
                switch getRes{
                case .failure(let err):
                    print("Debug print", err.localizedDescription)
                case .success(let getWeapons):
                    guard let getWeapons = getWeapons else { return }
                    DispatchQueue.main.async {
                        self.weapons = getWeapons
                    }
                }
            }
        }
    }
}
