import { HttpStateType, ReveData } from "../../../../util/CHttp";
import FF from "../../FF";
import FMap from "../../map/FMap";
import FAltarLight from "./FAltarLight";

/**
 * 祭坛
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FAltar extends cc.Component {

    @property({
        displayName: '对应的地图物件'
    })
    public mapGoodId:string = '27';

    @property({
        type:cc.Node,
        displayName: '点亮火焰动画'
    })
    public spine:cc.Node = null;

    @property({
        type:[FAltarLight],
        displayName: '祭坛灯柱'
    })
    public altarLight:Array<FAltarLight> = [];


    private map:FMap = null;

    onLoad () {
        this.map = this.node.parent.parent.getComponent(FMap);
    }

    start () {
        let ff = this.map.ff;
        let stage = ff.main.player.stage;
        if(stage.element.indexOf(this.mapGoodId) >= 0){
            this.spine.active = true;
        }else{
            this.spine.active = false;
        }
    }
    /**
     * 检查灯柱点亮情况
     */
    public check():boolean{
        for (let i = 0; i < this.altarLight.length; i++) {
            const element = this.altarLight[i];
            if(!element.spine.active){
                return false;
            }
        }
        this.altarOK();
        return true
    }
    /**
     * 祭坛亮起来
     */
    private altarOK(){
        this.getMapObject(this.mapGoodId);
    }
    /**
     * 捡起地图上的物品
     * @param objectId 
     */
    public getMapObject(objectId:string){
        let msg = {
            objectId:objectId
        }
        let ff = this.map.ff;
        ff.main.gameHttp.sendJson('stage/v1/stageObject',msg,(state,reve:ReveData)=>{
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    let player = ff.main.player;
                    let stage = player.stage;
                    stage.element.push(objectId);
                    let list = ff.main.sManage.getRewards(reve);
                    this.playAction(list);
                }else{
                    ff.main.showTips(reve.message);
                }
            }else{
                ff.main.showTips('网络异常');
            }
        });
    }

    private playAction(list){
        let ff = this.map.ff;
        ff.pauseSprite(true);
        this.spine.opacity = 0;
        this.spine.active = true;
        cc.tween(this.spine).sequence(
            cc.callFunc(()=>{
                this.moveCamera();
            }),
            cc.delayTime(0.7),
            cc.fadeIn(0.5),
            cc.callFunc(()=>{
                ff.pauseSprite(false);
                ff.addGoods(list,this.node.getPosition());
            })
        ).start();
    }
    private moveCamera(){
        let map = this.map;
        let camera = map.mCamera;

        let pos = cc.v3();
        let winsize = cc.winSize;
        pos.x = this.node.x -winsize.width/2;
        pos.y = this.node.y -winsize.height/2;

        cc.tween(camera.node).
        to(0.5,{position:pos}).start();
    }
}
