import { DataProgress, DataUser, StageProgress, TrophyRecord } from "../types/global";
import { StorageManager } from "./StorageManager"


export class DOUser {

    private data: DataUser
    private mapDones: Map<string, number> = new Map()

    constructor(dataUser: DataUser) {
        this.data = dataUser
        for (var i=0; i<this.data.dones.length; i++) {
            var obj = this.data.dones[i]
            this.mapDones.set(obj.stageID, obj.progress)
        }
    }
    public getData(): DataUser {
        return this.data
    }

    //====================================持久化数据
    public get_done(stageID: string): number {
        if (this.mapDones.has(stageID)) {
            return this.mapDones.get(stageID)
        }
        return 0
    }
    public get_doneCountByDate(year: number, month: number): number {
        var count = 0
        var key = `${year}-${month}`
        this.mapDones.forEach((progres: number, stageID: string)=>{
            if (stageID.startsWith(key) && progres >= 1) {
                count ++
            }
        })
        return count
    }
    public get_isAllDoneByDate(year: number, month: number): boolean {
        var boolean = true
        var key = `${year}-${month}`
        this.mapDones.forEach((progres: number, stageID: string)=>{
            if (stageID.startsWith(key) && progres < 1) {
                boolean = false
            }
        })
        return boolean
    }
    public update_stage_done(stageID: string, progress: number): void {
        var find: StageProgress
        for (var i=0; i<this.data.dones.length; i++) {
            var obj = this.data.dones[i]
            if (obj.stageID == stageID) {
                find = obj
                break
            }
        }
        if (find) {
            find.progress = progress
        }
        else {
            find = {stageID: stageID, progress: progress}
            this.data.dones.push(find)
        }
        this.mapDones.set(find.stageID, find.progress)
        StorageManager.getInstance().onUserChanged()
    }

    //================奖杯获得记录
    public get_trophyRecord(year: number, month: number): TrophyRecord {
        var find: TrophyRecord
        for (var i=0; i<this.data.trophy_records.length; i++) {
            var obj = this.data.trophy_records[i]
            if (obj.year == year && obj.month == month) {
                find = obj
                break
            }
        }
        return find
    } 
    public get_trophyHasNew(): TrophyRecord {
        var find: TrophyRecord
        for (var i=0; i<this.data.trophy_records.length; i++) {
            var obj = this.data.trophy_records[i]
            if (obj.isNew) {
                find = obj
                break
            }
        }
        return find
    } 
    public set_trophyNew(record: TrophyRecord, isNew: boolean): void {
        if (record.isNew != isNew) {
            record.isNew = isNew
            StorageManager.getInstance().onUserChanged()
        }
    }
    public get_trophyHasUnread(): boolean {
        var unread = false
        for (var i=0; i<this.data.trophy_records.length; i++) {
            var obj = this.data.trophy_records[i]
            if (obj.read == false) {
                unread = true
                break
            }
        }
        return unread
    } 
    public set_trophyAllread(): void {
        var changed = false
        for (var i=0; i<this.data.trophy_records.length; i++) {
            var obj = this.data.trophy_records[i]
            if (obj.read == false) {
                obj.read = true
                changed = true
            }
        }
        if (changed) {
            StorageManager.getInstance().onUserChanged()
        }
    }
    public add_trophy_record(year: number, month: number, day: number): void {
        var find
        for (var i=0; i<this.data.trophy_records.length; i++) {
            var obj = this.data.trophy_records[i]
            if (obj.year == year && obj.month == month) {
                find = obj
                break
            }
        }
        if (!find) {
            this.data.trophy_records.push({year: year, month: month, day: day, isNew: true, read: false})
            StorageManager.getInstance().onUserChanged()
        }
    }

    //=======关卡难度进度
    public get_progress(difficulty: string): number {
        var find: DataProgress
        for (var i=0; i<this.data.progresses.length; i++) {
            var obj = this.data.progresses[i]
            if (obj.difficulty == difficulty) {
                find = obj
                break
            }
        }
        if (find) {
            return find.index
        }
        return 0
    }
    public update_progress(difficulty: string, index: number): void {
        var find: DataProgress
        for (var i=0; i<this.data.progresses.length; i++) {
            var obj = this.data.progresses[i]
            if (obj.difficulty == difficulty) {
                find = obj
                break
            }
        }
        if (find) {
            find.index = index
        }
        else {
            find = {difficulty: difficulty, index: index}
            this.data.progresses.push(find)
        }
        StorageManager.getInstance().onUserChanged()
    }

    //====================================持久化数据
}