//
//  AppSettings.swift
//  PadelClub
//
//  Created by Razmig Sarkissian on 26/03/2024.
//

import Foundation
import LeStorage
import SwiftUI

@Observable
final class AppSettings: MicroStorable {
    
    var lastDataSource: String? = nil
    var didCreateAccount: Bool = false
    var didRegisterAccount: Bool = false
    
    //search tournament stuff
    var tournamentCategories: Set<TournamentCategory.ID>
    var tournamentLevels: Set<TournamentLevel.ID>
    var tournamentAges: Set<FederalTournamentAge.ID>
    var tournamentTypes: Set<FederalTournamentType.ID>
    var startDate: Date
    var endDate: Date
    var city: String
    var distance: Double
    var sortingOption: String
    var nationalCup: Bool
    var dayDuration: Int?
    var dayPeriod: DayPeriod
    
    func lastDataSourceDate() -> Date? {
        guard let lastDataSource else { return nil }
        return URL.importDateFormatter.date(from: lastDataSource)
    }
    
    func localizedLastDataSource() -> String? {
        guard let lastDataSource else { return nil }
        guard let date = URL.importDateFormatter.date(from: lastDataSource) else { return nil }
        
        return date.monthYearFormatted
    }

    func resetSearch() {
        tournamentAges = Set()
        tournamentTypes = Set()
        tournamentLevels = Set()
        tournamentCategories = Set()
        city = ""
        distance = 30
        startDate = Date()
        endDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())!
        sortingOption = "dateDebut+asc"
        nationalCup = false
        dayDuration = nil
        dayPeriod = .all
    }

    required init() {
        tournamentAges = Set()
        tournamentTypes = Set()
        tournamentLevels = Set()
        tournamentCategories = Set()
        city = ""
        distance = 30
        startDate = Date()
        endDate = Calendar.current.date(byAdding: .month, value: 3, to: Date())!
        sortingOption = "dateDebut+asc"
        nationalCup = false
        dayDuration = nil
        dayPeriod = .all
    }
    
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)
        lastDataSource = try container.decodeIfPresent(String.self, forKey: ._lastDataSource)
        didCreateAccount = try container.decodeIfPresent(Bool.self, forKey: ._didCreateAccount) ?? false
        didRegisterAccount = try container.decodeIfPresent(Bool.self, forKey: ._didRegisterAccount) ?? false
        tournamentCategories = try container.decodeIfPresent(Set<TournamentCategory.ID>.self, forKey: ._tournamentCategories) ?? Set()
        tournamentLevels = try container.decodeIfPresent(Set<TournamentLevel.ID>.self, forKey: ._tournamentLevels) ?? Set()
        tournamentAges = try container.decodeIfPresent(Set<FederalTournamentAge.ID>.self, forKey: ._tournamentAges) ?? Set()
        tournamentTypes = try container.decodeIfPresent(Set<FederalTournamentType.ID>.self, forKey: ._tournamentTypes) ?? Set()
        startDate = try container.decodeIfPresent(Date.self, forKey: ._startDate) ?? Date()
        endDate = try container.decodeIfPresent(Date.self, forKey: ._endDate) ?? Calendar.current.date(byAdding: .month, value: 3, to: Date())!
        city = try container.decodeIfPresent(String.self, forKey: ._city) ?? ""
        distance = try container.decodeIfPresent(Double.self, forKey: ._distance) ?? 30
        sortingOption = try container.decodeIfPresent(String.self, forKey: ._sortingOption) ?? "dateDebut+asc"
        nationalCup = try container.decodeIfPresent(Bool.self, forKey: ._nationalCup) ?? false
        dayDuration = try container.decodeIfPresent(Int.self, forKey: ._dayDuration)
        dayPeriod = try container.decodeIfPresent(DayPeriod.self, forKey: ._dayPeriod) ?? .all
    }
    
    enum CodingKeys: String, CodingKey {
        case _lastDataSource = "lastDataSource"
        case _didCreateAccount = "didCreateAccount"
        case _didRegisterAccount = "didRegisterAccount"
        case _tournamentCategories = "tournamentCategories"
        case _tournamentLevels = "tournamentLevels"
        case _tournamentAges = "tournamentAges"
        case _tournamentTypes = "tournamentTypes"
        case _startDate = "startDate"
        case _endDate = "endDate"
        case _city = "city"
        case _distance = "distance"
        case _sortingOption = "sortingOption"
        case _nationalCup = "nationalCup"
        case _dayDuration = "dayDuration"
        case _dayPeriod = "dayPeriod"
    }
}
