import pg from "../pg";
import { ITEMS, USER } from "../config/config"
import user from "../model/user";
import kitchen from "../model/kitchen";
import shop from "../model/shop";
//获取信息，购买物品，使用物品（吃东西），穿戴衣服/更换家具
class Api {
    static askUser() {
        return new Promise((resolve, reject) => {
            console.log(`[askUser] ${cc.find('middleLayer').getComponent('middleLayer').baseUrl}/pets/info`)
            pg.http.send("GET", `${cc.find('middleLayer').getComponent('middleLayer').baseUrl}pets/info`, null).then((userInfo: any) => {
                // let userInfo = USER;
                try{
                    const mood = (Date.now() - userInfo.status.eat.time) > 4 * 60 * 60 * 1000 ? 1: 0; // 4 hours
                    const info = {
                        name: userInfo.name,
                        coin: userInfo.coins,
                        level: userInfo.level,
                        eatTime: userInfo.status.eat.time,
                        useFurniture: userInfo.furniture,
                        useClothes: userInfo.dress,
                        growth: userInfo.current_exp,
                        growthLevel: userInfo.need_exp,
                        mood
                    };
                    console.log(info);
                    user.parse(info);
                    // let kitchenInfo = ITEMS;
                    kitchen.parse(userInfo.my_items);
                    shop.parse(userInfo.shop_items);
                    resolve('');
                } catch(e) {
                    reject(e)
                }
            }).catch(e => {
                reject(e)
            })
        });
    }
    static askUseItem(data) {
        return new Promise((resolve, reject) => {
            pg.http.send("POST", `${cc.find('middleLayer').getComponent('middleLayer').baseUrl}pets/use/item`, {item_id: data.id}).then((resp: any) => {
                // user.addGrowth(data.growthValue);
                // kitchen.use(data.id);
                try {
                    resolve(resp);
                } catch (e) {
                    reject(e)
                }
            }).catch(e => {
                reject(e)
            })
        });
    }
    static askBuyItem(data) {
        return new Promise((resolve, reject) => {
            data.id;
            data.type;
            pg.http.send("POST", `${cc.find('middleLayer').getComponent('middleLayer').baseUrl}pets/item/buy`, {item_id: data.id}).then((resp: any) => {
                // user.useCoin(data.cost * data.discount);
                // kitchen.buy(data.id);
                try {
                    resolve(resp);
                } catch (e) {
                    reject(e)
                }

            }).catch(e => {
                reject(e)
            })
        });
    }
}
export default Api;