import { ActivityModelType } from '../../db/Activity';
import { ActivityGroupShopRecType } from '../../db/ActivityGroupShopRec';
import { ActivityGroupShopUserRecType } from '../../db/ActivityGroupShopUserRec';
import { ActivityBase } from './activityField';

// 数据库
interface GroupShopTimerInDb {
    time: number;     // 活动开始之后的时间
    sum: number;      // 如果次数不足sum次则强行设成sum次
}

interface GroupShopDiscountInDb {
    id: number;        // 折扣id
    sum: number;       // 累计购买次数 
    discount: number;  // 显示折扣
    price: number;     // 真实价格
}

interface GroupShopItemInDb {
    itemId: number;       // 唯一id
    id: number;      // 物品id
    count: number;    // 一次购买能购买多少
    max: number;      // 单人可以购买多少次
    price: number;    // 无折扣单价
    discount: GroupShopDiscountInDb[];
    timers: GroupShopTimerInDb[]; // 定时器
}

interface GroupShopInDb {
    items: GroupShopItemInDb[];
}

// 数据
class GroupShopTimer {
    time: number;     // 时间戳
    itemId: number;   // items唯一id
    sum: number;      // 如果次数不足sum次则强行设成sum次
    
    constructor(beginTime: number, id: number, data: GroupShopTimerInDb) {
        this.time = beginTime + data.time * 60 * 60 * 1000;
        this.sum = data.sum;
        this.itemId = id;
    }
}

export class GroupShopDiscount {
    id: number = 0;        // 折扣唯一id
    sum: number = 0;       // 累计购买次数 
    discount: number = 0;  // 显示折扣
    price: number = 0;     // 真实价格

    constructor(price: number, data?: GroupShopDiscountInDb) {
        if(data) {
            this.id = data.id;
            this.sum = data.sum;
            this.discount = data.discount;
            this.price = data.price;
        } else {
            this.price = price;
        }
    }
}

class GroupShopItem {
    itemId: number;            // 唯一id
    id: number;           // 物品id
    count: number;         // 一次购买能购买多少
    max: number;           // 单人可以购买多少次
    price: number;         // 无折扣单价
    discount: GroupShopDiscount[] = [];
    sum: number = 0;       // 总价格
    curDiscount: GroupShopDiscount;
    hasBoughtCnt: number = 0;  // 玩家购买次数

    constructor(item: GroupShopItemInDb) {
        this.itemId = item.itemId;
        this.id = item.id;
        this.count = item.count;
        this.max = item.max;
        this.price = item.price;
        if(item.discount && item.discount.length > 0) {
            for(let discount of item.discount) {
                this.discount.push(new GroupShopDiscount(item.price, discount));
                if(!this.curDiscount) this.curDiscount = new GroupShopDiscount(item.price, discount);
            }
        }
    }
    
    setSum(sum: number) {
        this.sum = sum;
        this.checkDiscountBySum(sum);
    }

    checkDiscountBySum(sum: number) {
        for(let obj of this.discount) {
            if(obj.sum > sum) break;
            this.curDiscount = new GroupShopDiscount(this.price, obj);
        }
    }

    setPlayerCnt(cnt: number) {
        this.hasBoughtCnt = cnt;
    }

    checkBuyCnt(buyCnt: number) {
        return this.hasBoughtCnt + buyCnt <= this.max;
    }

    getCurDiscount() {
        return this.curDiscount? this.curDiscount: new GroupShopDiscount(this.price);
    }
}

// 团购活动数据
export class GroupShopData extends ActivityBase {
    items: GroupShopItem[] = [];
    timer: GroupShopTimer[] = [];
    private itemMap: Map<number, number> = new Map(); // id => index
    
    constructor(activityData: ActivityModelType, createTime: number, serverTime: number) {
        super(activityData, createTime, serverTime);
        this.initData(activityData.data)
    }

    public initData(data: string) {
        let dataObj: GroupShopInDb = JSON.parse(data);
        if(!dataObj.items || dataObj.items.length <= 0) return;

        for(let item of dataObj.items) {
            this.items.push(new GroupShopItem(item));
            this.itemMap.set(item.itemId, this.items.length - 1);
            if(item.timers && item.timers.length > 0) {
                for(let timer of item.timers) {
                    this.timer.push(new GroupShopTimer(this.beginTime, item.itemId, timer));
                }
            }
        }
    }

    public findItemById(itemId: number) {
        let index = this.itemMap.get(itemId);
        return this.items[index];
    }

    public setRecords(data: ActivityGroupShopRecType[]) {
        for(let { itemId, sum } of data) {
            let item = this.findItemById(itemId);
            if(item) item.setSum(sum);
        }
    }

    public incAllRecord(data: ActivityGroupShopRecType) {
        if(!data) return false;
        let item = this.findItemById(data.itemId);
        let oldDiscountId = item.getCurDiscount().id;
        item.setSum(data.sum);
        let newDiscountId = item.getCurDiscount().id;
        return oldDiscountId != newDiscountId;
    }


    public setPlayerRecord(data: ActivityGroupShopUserRecType[]) {
        for(let { itemId, buyCnt } of data) {
            let item = this.findItemById(itemId);
            if(item) item.setPlayerCnt(buyCnt);
        }
    }
    public getShowResult() {
        return {
            ...this.getBaseKeys(),
            items: this.items,
        }
    }

}