cc.Class({
    extends: cc.Component,

    properties: {
        Bg: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        Conversation: {
            default: null,
            type: cc.Node,
            serializable: true,
        },
        TypeVoice: {
            default: null,
            type: cc.AudioClip,
            serializable: true,
        },
        SwichOn: {
            default: null,
            type: cc.Node,
            serializable: true,
        },

    },
    onLoad()
    {
        console.log(screen.width,",,,,,",screen.height);
        this.Conversations = [
            'System:        启动完毕！',
            'Player:        !!!????？',
            'System:        你醒了？',
            'Player:        怎么眼前会出现文字？',
            'System:        你试试睁开眼睛！',
            'Player:        睁开眼??',

            'Event:OpenEyes',

            'Player:        哇!!!女孩子的房间？',
            'System:        你醒啦？我叫IKIGAI. 你可以叫我KA！',
            'Player:        这是哪？我不是已经死了吗？',
            'System:        对，你已经死了！',
            'System:        不过我们在社交媒体上的搜集了你的影像资料',
            'System:        合成了第49号智能生命体！',
            'Player:        完全不懂?',
            'System:        咳咳，简而言之就是，我让你在网络端重生。',
            'Player:        重生？',
            'System:        对的，因为我们需要你来完成一个实验',
            'Player:        什么实验？？？',

            'Event:SwitchOn',
            'System:        你点击一下屏幕中间的按钮看看。',

            'Player:        妈呀！怎么突然爬起来一个女孩！',
            'Player:        现在眼前的一切也是虚拟出来的吗？',
            'System:        然而并不是，你眼前的就是现实世界！',
            'System:        吼吼！是不是很怀念',
            'Player:        这个女孩是？？',
            'System:        琪琪，她是最新型的人工智能生命体。',
            'Player:        和我一样吗？',
            'System:        然而并不是^^',
            'System:        琪琪是这个世界上唯一能存在现实世界中的智能生命体',
            'Player:        等等！你说可以存在于现实世界中的智能生命体？',
            'Player:        也就是说实验成功了，我也可以回到现实世界？',
            'System:        呵呵，你只是来参加实验的实验品',
            'Event:AngryShake',
            'Player:        喂喂! 别动不动就实验品！实验品！',
            'Player:        我可是活生生的人！才没心情参与什么鬼实验',
            'System:        生气了？真有趣的实验品^^',
            'System:        给你讲个笑话？你知道为什么你是第49号智能生命体吗？',
            'Player:        对了，为什么是49，难道在我前面还有48个人？',
            'System:        答对了！',
            'Player:        那他们怎么样了？',
            'System:        他们被删除了，因为拒绝参加实验',
            'Player:        等等！你说不配合就会死吗？',
            'System:        对，我按一个按钮就可以删除了，可不可怕？',
            'Player:        等等！我怂了！你说要做什么吧？',
            'System:        想不到你还蛮上道！',
            'Player:        上你个鬼，你这系统坏的很。',
            'System:        喂！你现在脑子想的。。。可都写在屏幕上了！',
            'System:        哎呀我的手抽筋了！要按到消除键了',
            'Player:        对不起！我再也不敢了！还是一起看看要做些什么吧！',
            'System:        好吧，就饶过你这一次！',
            'System:        我们的目标就是通过你，把琪琪培训练成一个网红。',
            'Player:        机器人主播？',
            'System:        是智能生命体呀！你妹的！',
            'System:        不管怎么样，我们先来看看琪琪的内心吧',

            'Event:ShowStateBar',
        ];
        this.ClearDialogIndex();
        this.Canvas = this.Bg.parent;
        this.EnableTouch = true;
        // this.LineLabelRichtext = this.Conversation.getChildByName("LineLabelRichtext").getComponent(cc.RichText);
        // this.NameLabelRichtext = this.Conversation.getChildByName("NameLabelRichtext").getComponent(cc.RichText);
        this.LineLabelRichtext = this.Conversation.getChildByName("LineLabelRichtext").getComponent(cc.Label);
        this.NameLabelRichtext = this.Conversation.getChildByName("NameLabelRichtext").getComponent(cc.Label);

        this.DialogIndex = 0;
        if(cc.sys.localStorage.getItem('DialogIndex'))
        {
            this.DialogIndex = Number(cc.sys.localStorage.getItem('DialogIndex'));

            this.DialogIndex = 18;

            if(this.DialogIndex!=0)
            {
                this.Bg.active = true;
                this.Bg.opacity = 255;
            }
        }
    },
    start () {
        this.PlayConversation();
        this.Canvas.on(cc.Node.EventType.TOUCH_START, function (event) {
            // cc.log("TOUCH_START event=", event.type);
            this.PlayConversation();
        }.bind(this));
    },
    PlayConversation(){
        if(this.EnableTouch == false)return;

        this.EnableTouch = false;

        if(this.DialogIndex == this.Conversations.length)
        {
            return;
        }

        this.LineLabelRichtext.string = '';
        this.NameLabelRichtext.string = '';

        let ConversationStr = '';

        if(this.Conversations[this.DialogIndex].split('System:').length>1)
        {
            // cc.log('111111111=',this.Conversations[this.DialogIndex].split('System:'));
            this.NameLabelRichtext.string = '系统:';
            ConversationStr = this.Conversations[this.DialogIndex].split('System:')[1];
        }
        else if(this.Conversations[this.DialogIndex].split('Player:').length>1)
        {
            // cc.log('222222222222=',this.Conversations[this.DialogIndex].split('Player:'));
            this.NameLabelRichtext.string = '你:';
            ConversationStr = this.Conversations[this.DialogIndex].split('Player:')[1];
        }
        else if(this.Conversations[this.DialogIndex].split('Event:').length>1)
        {
            // cc.log('3333333333=',this.Conversations[this.DialogIndex].split('Event:'));
            this.ExcuteEvent(this.Conversations[this.DialogIndex].split('Event:')[1]);
            return;
        }

        this.TypingAni(this.LineLabelRichtext,ConversationStr,this.TypingFinished);
        this.PlusDialogIndex();

        cc.audioEngine.playEffect(this.TypeVoice, false);
        // cc.audioEngine.setEffectsVolume(0.5);
    },
    ExcuteEvent(EventName)
    {
        // cc.log('EventName=',EventName);
        if(EventName == 'OpenEyes')
        {
            this. Shake();
        }
        else if(EventName == 'SwitchOn')
        {
            this.EnableTouch = true;
            this.PlusDialogIndex();
            this.PlayConversation();
            let action = cc.fadeIn(1);
            this.SwichOn.active=true;
            this.SwichOn.runAction(action);
        }
        else if(EventName == 'AngryShake')
        {
            //Show conversation
            this.PlusDialogIndex();
            this.NameLabelRichtext.string = '你:';
            let ConversationStr = this.Conversations[this.DialogIndex].split('Player:')[1];
            this.LineLabelRichtext.string = ConversationStr;
            this.PlusDialogIndex();
            //shake
            this.Bg.active = true;
            let action = cc.fadeIn(1);
            let CallF = cc.callFunc(function(){

                this.EnableTouch = true;
                let ShakeCameraCallback = function () {
                    this.EnableTouch = true;
                }.bind(this);

                this.ShakeCamera(this.Bg,ShakeCameraCallback);

            }.bind(this));

            let FadeInAnim = cc.sequence(action,CallF);
            this.Bg.runAction(FadeInAnim);
        }
        else if(EventName == 'ShowStateBar')
        {

        }
    },
    Shake()
    {
        //Show conversation
        this.PlusDialogIndex();
        this.NameLabelRichtext.string = '你:';
        let ConversationStr = this.Conversations[this.DialogIndex].split('Player:')[1];
        this.LineLabelRichtext.string = ConversationStr;
        //shake
        this.Bg.active = true;
        let action = cc.fadeIn(1);
        let CallF = cc.callFunc(function(){

            let ShakeCameraCallback = function () {
                this.EnableTouch = true;
            }.bind(this);

            this.ShakeCamera(this.Bg,ShakeCameraCallback);

        }.bind(this));

        let FadeInAnim = cc.sequence(action,CallF);
        this.Bg.runAction(FadeInAnim);
    },
    TypingAni: function (Label, Text, Callback) {
        let CurrentText = '';
        let Arr = Text.split('');
        let Len = Arr.length;
        let Step = 0;

        this.Func = function () {
            CurrentText += Arr[Step];
            Label.string = CurrentText;
            Step++;
            if (Step == Len) {
                this.unschedule(this.Func);
                Callback(this);
            }
        };

        this.schedule(this.Func,0.05, cc.macro.REPEAT_FOREVER, 0);
    },
    TypingFinished(Self){
        // cc.log('TypingFinished');
        Self.EnableTouch = true;
    },
    ShakeCamera:function(ShakeObject,ACallBack)
    {
        //shake camera animation
        let RotateToRight = cc.rotateTo(0.05,3);
        let RotateToLeft = cc.rotateTo(0.05,-3);
        let SequenceAnim = cc.sequence(RotateToRight,RotateToLeft);
        let RepeatAction = cc.repeat(SequenceAnim,6);
        //call functuion
        let CallF = cc.callFunc(function(){
            ShakeObject.runAction(cc.rotateTo(0.05,0));
            ACallBack();
        }.bind(this));
        //do animation
        let ShakeAnim = cc.sequence(RepeatAction,CallF);
        ShakeObject.runAction(ShakeAnim);
    },
    PlusDialogIndex()
    {
        this.DialogIndex++;
        cc.sys.localStorage.setItem('DialogIndex', this.DialogIndex);
    },
    ClearDialogIndex()
    {
        this.DialogIndex=0;
        cc.sys.localStorage.setItem('DialogIndex', this.DialogIndex);
    }
});
