import i18n from "../../../i18n/i18n";
import ViewObject from "../../../main/ViewObject";
import { HttpStateType, ReveData } from "../../../util/CHttp";
import CUtil from "../../../util/CUtil";
import GoodItem from "../../common/GoodItem";
import Pack from "./Pack";
/**
 * 食物查看并使用
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FoodView extends ViewObject {

    @property(cc.Label)
    mName: cc.Label = null;

    @property(cc.Label)
    mAbout: cc.Label = null;

    @property(GoodItem)
    mGoodItem: GoodItem = null;

    @property(cc.Label)
    mCount: cc.Label = null;

    @property(cc.Label)
    mUserLabel: cc.Label = null;

    public pack:Pack
    public goodItem:GoodItem

    public initGood(pack:Pack,goodItem:GoodItem){
        this.pack = pack
        this.goodItem = goodItem
        this.mGoodItem.initGood(this.main,this.goodItem.data)
        this.mName.string = i18n.t(goodItem.good.name)
        this.mAbout.string = i18n.t(goodItem.good.about)
        if(goodItem.good.type == 3){
            this.mUserLabel.string = i18n.t('使用')
        }else if(goodItem.good.type == 5){
            this.mUserLabel.string = i18n.t('食用')
        }

        this.flush()
    }

    private flush(){
        if(this.goodItem.good.type == 5){
            let foodAttr = this.main.player.foodAttr
            let nowDate = CUtil.getNowDateInt()
            if(nowDate > foodAttr.date){
                foodAttr.date = nowDate
                foodAttr.count = 0
            }
            this.mCount.string = i18n.t('今日食用食物上限')+":"+foodAttr.count+"/10"
        }
    }


    public onclickEat(){
        if(this.goodItem.good.type == 3){
            this.eatGift()
        }else if(this.goodItem.good.type == 5){
            this.eatFood()
        }
    }

    private eatFood(){
        let msg = {
            id:this.goodItem.good.id
        }
        this.main.startLoad()
        this.main.gameHttp.sendJson('eatFood/v1/eat',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.main.player.foodAttr = reve.data._foodAttr
                    this.pack.flushGood()
                    this.flush()
                    this.mGoodItem.data.count -= 1
                    if(this.mGoodItem.data.count <= 0){
                        this.exitDistroy()
                    }else{
                        this.mGoodItem.flushGood()
                    }
                    this.main.showTips('成功食用');
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }
    private eatGift(){
        let msg = {
            id:this.goodItem.good.id
        }
        this.main.gameHttp.sendJson('eatFood/v1/gift',msg,(state,reve:ReveData)=>{
            this.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    this.pack.flushGood()
                    this.flush()
                    this.mGoodItem.data.count -= 1
                    if(this.mGoodItem.data.count <= 0){
                        this.exitDistroy()
                    }else{
                        this.mGoodItem.flushGood()
                    }
                    this.main.showReward(reve)
                }else{
                    this.main.showTips(reve.message);
                }
            }else{
                this.main.showTips('网络异常');
            }
        });
    }


}
