import { HttpStateType, ReveData } from "../../../../util/CHttp";
import FF from "../../FF";
import FSprite from "../../object/FSprite";
import WOneByone from "../map1/WOneByone";
import FFruit26 from "./FFruit26";

/**
 * 摘果子任务 奖励编号26
 */
const {ccclass, property} = cc._decorator;

@ccclass
export default class FDialogPack26 extends cc.Component {

    @property(cc.Prefab)
    mMapDialog: cc.Prefab = null;

    @property({
        type:cc.Node,
        displayName: '任务提示'
    })
    public iconTouch:cc.Node = null;

    @property({
        displayName: '收集的道具id'
    })
    public goodId = 3002;

    @property({
        type:[FFruit26],
        displayName: '收集的地图上的果子'
    })
    public goodNodes:Array<FFruit26> = [];

    /**
     * 记录当前状态
     * 0:从来没有过对话
     * 1:对话过一次
     */
    public state:number = 0;

    private ff:FF;
    onLoad() {
        for (let i = 0; i < this.goodNodes.length; i++) {
            const element = this.goodNodes[i];
            element.setPick26(this);
        }
    }

    onBeginContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.showButton();
            }
        }
    }

    onEndContact(contact: cc.PhysicsContact, self: cc.PhysicsCollider, other: cc.PhysicsCollider){
        if(other.node.group == 'A'){
            let obj = other.node.getComponent(FSprite);
            this.ff = obj.ff;
            if(obj == this.ff.mainSprite){
                this.closeButton();
            }
        }
    }
    public showButton(){
        this.ff.control.mEventButton.node.active = true;
        this.ff.control.mEventButton.setCallback(()=>{
            this.startStory();
        });
    }
    public closeButton(){
        this.ff.control.mEventButton.node.active = false;
    }

    public startStory(){
        /**
         * 暂停所有
         */
        this.iconTouch.active = false;
        this.ff.pauseSprite(true);
        this.ff.mBlockInputEvents.active = true;
        if(this.state == 0){
            this.showDialog1();
        }else{
            let count = this.ff.mFFheader.getTmpCount(this.goodId);
            if(count >= this.goodNodes.length){
                this.showDialogEnd();
            }else{
                this.showDialog1_x();
            }
            
        }
    }

    private showDialog1(){
        let dialogs = [
            '我是美神，我最近新学了一些秀法',
            '如果你能给我4块棉布，我就能给你变出漂亮的衣服',
        ];
        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            this.iconTouch.active = true;
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
            this.state = 1;
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private showDialog1_x(){
        let dialogs = [
            '还没找到吗?',
        ];
        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            this.iconTouch.active = true;
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }
    private showDialogEnd(){
        let dialogs = [
            '衣服做好了，快穿上看看吧',
        ];
        let node = cc.instantiate(this.mMapDialog);
        node.group = 'map'
        node.zIndex = 9999;
        node.x = this.node.x;
        node.y = this.node.y + this.node.height;
        node.parent = this.ff.mMap.mSprites;
        let obo = node.getComponent(WOneByone);
        
        obo.dialogs = dialogs;
        obo.setCallback(()=>{
            this.iconTouch.active = false;
            node.destroy();
            this.ff.setBlockInputCallback(null);
            this.ff.pauseSprite(false);
            this.ff.mBlockInputEvents.active = false;
            this.ff.mFFheader.removeTmpGood(this.goodId,this.goodNodes.length);
            this.getMapObject(this.node.name);

        });
        this.ff.setBlockInputCallback(()=>{
            obo.jump();
        });
        obo._start();
    }

    /**
     * 捡起地图上的物品
     * @param objectId 
     */
    public getMapObject(objectId:string){
        let msg = {
            objectId:objectId
        }
        this.ff.main.gameHttp.sendJson('stage/v1/stageObject',msg,(state,reve:ReveData)=>{
            this.ff.main.stopLoad();
            if(state == HttpStateType.SUCCESS){
                if(reve.retCode == 0){
                    let player = this.ff.main.player;
                    let stage = player.stage;
                    stage.element.push(objectId);
                    
                    let list = this.ff.main.sManage.getRewards(reve);
                    this.ff.addGoods(list,this.node.getPosition());
                    this.iconTouch.active = false;
                    this.ff.mFFheader.removeTmpGood(this.goodNodes,this.goodNodes.length);
                    this.node.removeComponent(cc.PhysicsBoxCollider);
                }else{
                    this.ff.main.showTips(reve.message);
                }
            }else{
                this.ff.main.showTips('网络异常');
            }
        });
    }
}
