/*
 *
 * Copyright (c) 2025. Jeffery Hsu - Achievement Report Generator
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 *
 */

import { IAssessment, IAssessmentMethod } from '@renderer/types'

export class AssessmentMethod implements IAssessmentMethod {
  private _method: string
  private _weight: number

  constructor(method: string, weight: number) {
    this._method = method
    this._weight = weight
  }

  public static factory() {
    return new AssessmentMethod('', 0)
  }

  public static factoryFromImpl(impl: IAssessmentMethod) {
    return new AssessmentMethod(impl.method, Number(impl.weight))
  }

  public static factoryToImpl(): IAssessmentMethod {
    return {
      method: '',
      weight: 0
    }
  }

  public setFromImpl(impl: IAssessmentMethod) {
    this._method = impl.method
    this._weight = Number(impl.weight)
  }

  public getCurrImpl(): IAssessmentMethod {
    return { method: this._method, weight: this.weight }
  }

  get method(): string {
    return this._method
  }

  set method(value: string) {
    this._method = value
  }

  get weight(): number {
    return this._weight
  }

  set weight(value: number) {
    this._weight = value
  }
}

export class AssessmentData implements IAssessment {
  private readonly _method: AssessmentMethod[]
  private _stage: string
  private _weight: number

  constructor(stage: string, weight: number | string, method?: AssessmentMethod[]) {
    this._method = method ?? []
    this._stage = stage
    this._weight = Number(weight)
  }

  public static factoryToImpl(): IAssessment {
    return { method: [], stage: '', weight: 0 }
  }

  get method(): AssessmentMethod[] {
    return this._method
  }

  get stage(): string {
    return this._stage
  }

  set stage(value: string) {
    this._stage = value
  }

  get weight(): number {
    return this._weight
  }

  set weight(value: number | string) {
    this._weight = Number(value)
  }

  public addMethod(method: AssessmentMethod) {
    this._method.push(method)
  }

  public modifyMethod(method: AssessmentMethod, idx: number) {
    if (idx < 0 || idx >= this._method.length) {
      return
    }
    this._method[idx] = method
  }

  public removeMethod(idx: number) {
    this.method.splice(idx, 1)
  }

  public static factory() {
    return new AssessmentData('', 0, [])
  }
}

export class Assessment {
  private _daily: AssessmentData
  private _progress: AssessmentData
  private _final: AssessmentData

  constructor(daily: AssessmentData, progress: AssessmentData, final: AssessmentData) {
    this._daily = daily
    this._progress = progress
    this._final = final
  }

  public static factory() {
    return new Assessment(
      AssessmentData.factory(),
      AssessmentData.factory(),
      AssessmentData.factory()
    )
  }

  public checkTotalWeight() {
    return this._daily.weight + this._progress.weight + this._final.weight === 100
  }

  get daily(): AssessmentData {
    return this._daily
  }

  set daily(value: AssessmentData) {
    this._daily = value
  }

  get progress(): AssessmentData {
    return this._progress
  }

  set progress(value: AssessmentData) {
    this._progress = value
  }

  get final(): AssessmentData {
    return this._final
  }

  set final(value: AssessmentData) {
    this._final = value
  }
}
