import { playAudioByUrl } from "./ym_demo_fk_util";
import { showFireworks } from "./ym_demo_fk_utils";

cc.Class({
    extends: cc.Component,

    properties: {
        // foo: {
        //     // ATTRIBUTES:
        //     default: null,        // The default value will be used only when the component attaching
        //                           // to a node for the first time
        //     type: cc.SpriteFrame, // optional, default is typeof default
        //     serializable: true,   // optional, default is true
        // },
        // bar: {
        //     get () {
        //         return this._bar;
        //     },
        //     set (value) {
        //         this._bar = value;
        //     }
        // },
        layerOver: cc.Prefab,


    },

    // LIFE-CYCLE CALLBACKS:

    // onLoad () {},
    onLoad() {
        cc.debug.setDisplayStats(false);
        this.initSceneData();
        this.initSize();
    },

    initSceneData() {
        this._imageResList = [];
        this._audioResList = [];
        this._animaResList = [];
        this._textResList = [];
    },

    _designSize: null,
    _frameSize: null,
    _mapScaleMin: null,
    _mapScaleMax: null,
    _cocosScale: null,
    initSize() {


        // 注意cc.winSize只有在适配后(修改fitHeight/fitWidth后)才能获取到正确的值,因此使用cc.getFrameSize()来获取初始的屏幕大小
        let screen_size = cc.view.getFrameSize().width / cc.view.getFrameSize().height
        let design_size = cc.Canvas.instance.designResolution.width / cc.Canvas.instance.designResolution.height
        let f = screen_size >= design_size
        cc.Canvas.instance.fitHeight = f
        cc.Canvas.instance.fitWidth = !f

        const frameSize = cc.view.getFrameSize();
        this._frameSize = frameSize;

        this._designSize = cc.view.getDesignResolutionSize();

        let sx = cc.winSize.width / frameSize.width;
        let sy = cc.winSize.height / frameSize.height;
        this._cocosScale = Math.min(sx, sy);

        sx = frameSize.width / this._designSize.width;
        sy = frameSize.height / this._designSize.height;
        this._mapScaleMin = Math.min(sx, sy) * this._cocosScale;
        this._mapScaleMax = Math.max(sx, sy) * this._cocosScale;




        cc.log('cc.Canvas: ', cc.Canvas);
    },

    start() {
        let getData = this.getData.bind(this);
        if (window && window.courseware) {
            // console.log('has window');
            getData = window.courseware.getData;
        }
        getData((data) => {
            console.log('data:', data);
            this.data = data || this.getDefaultData();
            this.data = JSON.parse(JSON.stringify(this.data));

            var newArr = [];
            this.data.contentObj.picArr.forEach(function (item, idx, arr) {

                const myItem = { "title": item.left.title, "pic_url": item.left.pic_url, "audio_url": item.left.audio_url, "cardId": idx + 1000, "radioValue": item.left.radioValue };
                const myItem2 = { "title": item.right.title, "pic_url": item.right.pic_url, "audio_url": item.right.audio_url, "cardId": idx + 1000, "radioValue": item.right.radioValue };
                newArr.push(myItem);
                newArr.push(myItem2);
            });
            console.log(newArr, 'fffffffffffff');
            this.data.contentObj.picArr = newArr;
            this.data.contentObj.picArr.sort(function () {
                return .5 - Math.random();
            });
            this.preloadItem()
        })
    },


    _imageResList: null,
    _audioResList: null,
    _animaResList: null,
    _cardIdResList: null,
    _textResList: null,
    _titleUrl: null,
    _titleStr: null,
    preloadItem() {
        this.addPreloadImage();
        this.addPreloadAudio();
        // this.addPreloadAnima();
        this.preload();
    },

    getData(func) {
        func(this.getDefaultData());
    },

    getDefaultData() {

        // const dataJson = ' {"contentObj":{"picArr":[{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"","pic_url":"","audio_url":""},"right":{"title":"","pic_url":"","audio_url":""}}],"audio_url":"","title":""},"audio_url":""}';
        // const dataJson = '{"contentObj":{"picArr":[{"left":{"title":"11111","pic_url":"http://staging-teach.cdn.ireadabc.com/156b8293a073ef828f6e01fc620c3562.png","audio_url":"http://staging-teach.cdn.ireadabc.com/83513f8d6083ebe072e3f9e47a21558a.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"222222","pic_url":"http://staging-teach.cdn.ireadabc.com/e029bf3210a23a169f1cc14a851cc9e4.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"3333","pic_url":"http://staging-teach.cdn.ireadabc.com/89f8093ced2b9ddeccec352e9b7f2775.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/440cf9d57752b30691d90ed565e8c924.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"444444","pic_url":"http://staging-teach.cdn.ireadabc.com/5a2c61d8faf5935e70fd9b15953b12f6.png","audio_url":"http://staging-teach.cdn.ireadabc.com/a5fa20e0c6024421b0d9a7007c303503.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"5555","pic_url":"http://staging-teach.cdn.ireadabc.com/ce2e48ecefbf8b7011385a95133fe987.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/a41f0f2bf190396e2641ec8dada8edfe.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"66","pic_url":"http://staging-teach.cdn.ireadabc.com/13845267419e50bd7f0c0f74ba424be2.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/32258b839eca32b6ccf94f3dd6c86e0c.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"77777","pic_url":"http://staging-teach.cdn.ireadabc.com/7ec7599c86f974c9f88409893010811d.png","audio_url":"http://staging-teach.cdn.ireadabc.com/49ac987d13695eb411da4bef0adb318f.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"8888888","pic_url":"http://staging-teach.cdn.ireadabc.com/24ee95ea61a2a653ce25c65557b18356.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/85cff126986f5cc11b05e95181da5869.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"99999999999999999","pic_url":"http://staging-teach.cdn.ireadabc.com/3aa5a936d8e0e6f1dd2f6eb6d3d26645.jpg","audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3"},"right":{"title":"","pic_url":"","audio_url":""}},{"left":{"title":"10101010101010","pic_url":"http://staging-teach.cdn.ireadabc.com/5ec43bf9507ff08d556487dee9234653.png","audio_url":"http://staging-teach.cdn.ireadabc.com/66878465ec50d162bd60fcdaa641933b.mp3"},"right":{"title":"","pic_url":"","audio_url":""}}],"audio_url":"","title":"sight words"}}'
        // const dataJson = ' {"contentObj":{"picArr":[{"left":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/c8abf7383d65bcbdba8f390785cece60.png","audio_url":"http://staging-teach.cdn.ireadabc.com/83513f8d6083ebe072e3f9e47a21558a.mp3"},"right":{"title":"111111","pic_url":"","audio_url":"http://staging-teach.cdn.ireadabc.com/83513f8d6083ebe072e3f9e47a21558a.mp3"}},{"left":{"title":"22222","pic_url":"","audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/f6f4bbef497409fef3c8f1d375893829.png","audio_url":"http://staging-teach.cdn.ireadabc.com/5285e1e88a26a08783757e9c43a8653f.mp3"}},{"cardId":"","left":{"title":"3333","pic_url":"","audio_url":"http://staging-teach.cdn.ireadabc.com/440cf9d57752b30691d90ed565e8c924.mp3"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/7b18a7931d64f7b9e8b7ecd25cb5e3d4.png","audio_url":"http://staging-teach.cdn.ireadabc.com/440cf9d57752b30691d90ed565e8c924.mp3"}},{"cardId":"","left":{"title":"444444","pic_url":"","audio_url":"http://staging-teach.cdn.ireadabc.com/a5fa20e0c6024421b0d9a7007c303503.mp3"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/e12d13282a10f92d65ac41b4aecaad4c.png","audio_url":"http://staging-teach.cdn.ireadabc.com/a5fa20e0c6024421b0d9a7007c303503.mp3"}},{"cardId":"","left":{"title":"555555555","pic_url":"","audio_url":"http://staging-teach.cdn.ireadabc.com/a41f0f2bf190396e2641ec8dada8edfe.mp3"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/991a9dd15e889ee4ff13538e49f6a0d7.png","audio_url":"http://staging-teach.cdn.ireadabc.com/a41f0f2bf190396e2641ec8dada8edfe.mp3"}}],"audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3","title":"123"},"audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3"}'
        const dataJson = ' {"contentObj":{"picArr":[{"cardId":"","left":{"title":"123123","pic_url":"http://staging-teach.cdn.ireadabc.com/1cb7f4dfd279ec8c9faf401ef3d5395b.jpg","audio_url":"","radioValue":"B"},"right":{"title":"111111","pic_url":"","audio_url":"","radioValue":"A"},"radioValue":"B"},{"cardId":"","left":{"title":"1111122222","pic_url":"http://staging-teach.cdn.ireadabc.com/8eda76334b178bbe56d1f79ff53e68d0.jpg","audio_url":"","radioValue":"A"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/34a119bfa22caf4785bacfe639d9fa05.png","audio_url":"","radioValue":"B"},"radioValue":"D"},{"cardId":"","left":{"title":"33333","pic_url":"http://staging-teach.cdn.ireadabc.com/991a9dd15e889ee4ff13538e49f6a0d7.png","audio_url":"","radioValue":"A"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/fb7ce5b740206aff8c3bb1f5cfc21ab9.png","audio_url":"","radioValue":"B"},"radioValue":"D"},{"cardId":"","left":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/c029a30bf72efaa8b6a0b9de76d5bc98.png","audio_url":"","radioValue":"B"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/156b8293a073ef828f6e01fc620c3562.png","audio_url":"","radioValue":"B"},"radioValue":"D"},{"left":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/dfbba6be5ffedc34d746634bd1be1b20.png","audio_url":"","radioValue":"B"},"right":{"title":"","pic_url":"http://staging-teach.cdn.ireadabc.com/3be3efc51e3e5178e3afc882c9a528d1.jpg","audio_url":"","radioValue":"B"},"radioValue":"D"}],"audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3","title":"123"},"audio_url":"http://staging-teach.cdn.ireadabc.com/a4d27ad173a95823d4b8ec82363c5aa0.mp3"}'
        const data1 = JSON.parse(dataJson);
        return data1;
    },

    preload() {

        const preloadArr = this._imageResList.concat(this._audioResList).concat(this._animaResList);
        cc.assetManager.loadAny(preloadArr, null, null, (err, data) => {
            console.log(' preload data, ', data);

            // typeof
            this.loadEnd();
            if (window && window["air"]) {
                window["air"].hideAirClassLoading();
            }
            this.myStart();
        });
    },
    addPreloadImage() {

        // this.data.contentObj.picArr.forEach(group => {
        //     this._imageResList.push({ url: group.left.pic_url });
        //     this._imageResList.push({ url: group.right.pic_url });
        //     this._textResList.push({ text: group.left.title });
        //     this._textResList.push({ text: group.right.title });
        // })
        var group = this.data.contentObj.picArr;
        for (let index = 0; index < group.length; index++) {
            this._imageResList.push({ url: group[index].pic_url, cardId: group[index].cardId, type: group[index].radioValue });
            this._textResList.push({ text: group[index].title, cardId: group[index].cardId, type: group[index].radioValue });

        }
        // console.log('=============', this._imageResList);
    },

    addPreloadAudio() {

        this.data.contentObj.picArr.forEach((item) => {
            this._audioResList.push({ url: item.audio_url });

        });
    },

    addPreloadAnima() {
        if (!this.data.contentObj.picArr) {
            return;
        }
        this.data.contentObj.picArr.forEach((group) => {

            if (group.hotZoneItemArr) {
                group.hotZoneItemArr.forEach((item) => {
                    if (item.gIdx == '0' & item.skeJsonData) {
                        this._animaResList.push({ url: item.skeJsonData.url });
                        this._animaResList.push({ url: item.texJsonData.url });
                        this._animaResList.push({ url: item.texPngData.url });
                    }
                })
            }

            if (group.skeJsonData) {
                this._animaResList.push({ url: group.skeJsonData.url });
                this._animaResList.push({ url: group.texJsonData.url });
                this._animaResList.push({ url: group.texPngData.url });
            }
        })
    },

    loadEnd() {
        // this.initData();
        // this.initAudio();
        // this.initView();
        // this.initListener();
    },
    _clickItemIdx: null,
    _clickItem2Idx: null,
    _firstCardNode: null,
    _flipSuccessCount: null,
    myStart() {


        this._flipSuccessCount = 0;

        const layoutNode = cc.find('Canvas/AutoContainer/layout');
        var layout = layoutNode.getComponent(cc.Layout);
        layoutNode.active = false;

        const playStartAudio = cc.find('Canvas/startsound').getComponent(cc.AudioSource);
        playStartAudio.play();

        const signNode = cc.find('Canvas/background/sign');
        const bg1Node = signNode.getChildByName('bg1');
        bg1Node.addComponent(cc.Button);
        var labelTitle = signNode.getChildByName('label').getComponent(cc.Label);
        const data = this.data;
        labelTitle.string = data.contentObj.title;

        var audioUrl = data.contentObj.audio_url;
        const playAudio = bg1Node.getComponent(cc.AudioSource);
        cc.assetManager.loadRemote(audioUrl, (err, audioClip) => {
            playAudio.clip = audioClip;
        });
        bg1Node.on('click', () => {
            playAudio.play();
        });

        var scaleRadio;
        if (this._imageResList.length > 8) {
            scaleRadio = 0.8;
            layout.spacingX = 120;
            // layout.paddingLeft = 60;
        } else {
            scaleRadio = 0.9;
            layout.spacingX = 180;
            // layout.paddingLeft = 100;
        }
        //开始按钮
        const playBtnNode = cc.find('Canvas/startButton/button');
        const playBtnAudio = cc.find('Canvas/startButton/sound').getComponent(cc.AudioSource);
        playBtnNode.on('click', () => {
            layoutNode.opacity = 1000;
            layoutNode.active = true;

            playBtnAudio.play();
            playBtnNode.active = false;
            playBtnNode.enabled = false;
            cc.find('Canvas/startButton').opacity = 0;

            var timeCallback = function (dt) {

                layoutNode.children.forEach(item => {
                    const tempNode = item.getChildByName('bgback');
                    cc.tween(item)
                        .to(0.3, { scaleX: 0 })
                        .call(() => {
                            tempNode.opacity = 1000;
                            // labelNode.opacity = 0;
                            // var playAudio = item.getComponent(cc.AudioSource);
                            // var flipButton = item.getComponent(cc.Button);
                            // flipButton.enabled = true;
                            // playAudio.play();
                        })
                        .to(0.3, { scaleX: scaleRadio })
                        .call(() => {
                            var playAudio = cc.find('Canvas/Card/bgback').getComponent(cc.AudioSource);
                            playAudio.play();
                        })
                        .start();
                });
            }
            this.scheduleOnce(timeCallback, 2);
        });

        for (let index = 0; index < this._imageResList.length; index++) {
            const cardNode = cc.instantiate(cc.find('Canvas/Card'));
            const imgNode = cardNode.getChildByName('img1');
            const bgbackNode = cardNode.getChildByName('bgback');
            bgbackNode.opacity = 0;
            console.log('type ===========', this._imageResList[index].type);
            if (this._imageResList[index].type == 'A') {
                // label 加载
                const labelNode = cardNode.getChildByName('label');
                var label = labelNode.getComponent(cc.Label);
                label.string = this._textResList[index].text;
            } else {
                // img 加载
                const img = imgNode.addComponent(cc.Sprite);
                var imgUrl = this._imageResList[index].url;
                cc.loader.load(imgUrl, function (err, texture) {
                    // Use texture to create sprite frame
                    
                    var maxWidth = 220;
                    var maxHeight = 220;
                    const spriteFrame = new cc.SpriteFrame(texture);
                    img.spriteFrame = spriteFrame;
                    var scaleX = maxWidth / imgNode.width;
                    var scaleY = maxHeight / imgNode.height;
                    imgNode.scale = Math.min(scaleX, scaleY);
                });
                // img添加按钮
                imgNode.addComponent(cc.Button);
                imgNode.on('click', () => {
                    var audioUrl = this._audioResList[index].url;
                    cc.assetManager.loadRemote(audioUrl, (err, audioClip) => {
                        const audioId = cc.audioEngine.play(audioClip, false, 0.8);
                        cc.audioEngine.setFinishCallback(audioId, () => {

                        });
                    });
                });

            }

            // 背景图添加按钮
            bgbackNode.addComponent(cc.Button);


            cardNode.scale = scaleRadio;
            layoutNode.addChild(cardNode);
            layout.updateLayout();

            bgbackNode.on('click', () => {

                var flipButton = bgbackNode.getComponent(cc.Button);
                flipButton.enabled = false;
                cc.tween(cardNode)
                    .to(0.3, { scaleX: 0 })
                    .call(() => {
                        bgbackNode.opacity = 0;
                        // labelNode.opacity = 0;
                    })
                    .to(0.3, { scaleX: scaleRadio })
                    .call(() => {
                        var audioUrl = this._audioResList[index].url;
                        if (!audioUrl) {
                            if (!this._clickItemIdx) {
                                this._clickItemIdx = this._imageResList[index].cardId;
                                this._firstCardNode = cardNode;

                            } else {
                                this._clickItem2Idx = this._imageResList[index].cardId;

                                if (this._clickItemIdx == this._clickItem2Idx) {
                                    const dragonNode = cardNode.getChildByName('dragon');
                                    let dragonAnimation = dragonNode.getComponent(dragonBones.ArmatureDisplay);
                                    dragonNode.opacity = 1000;
                                    dragonAnimation.playAnimation('', 1);

                                    const dragonNode2 = this._firstCardNode.getChildByName('dragon');
                                    let dragonAnimation2 = dragonNode2.getComponent(dragonBones.ArmatureDisplay);
                                    dragonNode2.opacity = 1000;
                                    dragonAnimation2.playAnimation('', 1);


                                    var playAudio = cardNode.getChildByName('rightsound').getComponent(cc.AudioSource);
                                    playAudio.play();
                                    //配对，无操作，播动画
                                    const copyNode = this._firstCardNode;
                                    this.scheduleOnce(() => {
                                        cardNode.opacity = 0;
                                        copyNode.opacity = 0;
                                        this._flipSuccessCount += 2;
                                        if (this._flipSuccessCount == this._imageResList.length) {
                                            console.log('游戏完成finish！！！！！！！！！');
                                            var playAudio = cc.find('Canvas/successsound').getComponent(cc.AudioSource);
                                            playAudio.play();
                                            showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(0, -400), cc.v2(0, 1000), 200, 200);
                                            showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(-600, -400), cc.v2(200, 1000), 200, 200);
                                            showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(600, -400), cc.v2(-200, 1000), 200, 200);

                                            const replayBtnNode = cc.find('Canvas/restartButton');
                                            replayBtnNode.opacity = 255;
                                            const playAudio2 = replayBtnNode.getChildByName('sound').getComponent(cc.AudioSource);
                                            const replayBtn = replayBtnNode.getChildByName('button');
                                            replayBtnNode.zIndex = 1111;
                                            replayBtn.on('click', () => {
                                                playAudio2.play();
                                                console.log('0000000000');
                                                cc.game.restart();
                                            });
                                            layoutNode.active = false;
                                        }
                                    }, 0.5);



                                } else {
                                    var playAudio = cardNode.getChildByName('wrongsound').getComponent(cc.AudioSource);
                                    playAudio.play();
                                    //错误，重置两个卡片
                                    cc.tween(cardNode)
                                        .to(0.3, { scaleX: 0 })
                                        .call(() => {
                                            bgbackNode.opacity = 1000;
                                            // labelNode.opacity = 0;
                                            // var playAudio = bgbackNode.getComponent(cc.AudioSource);
                                            var flipButton = bgbackNode.getComponent(cc.Button);
                                            flipButton.enabled = true;
                                            // playAudio.play();
                                        })
                                        .to(0.3, { scaleX: scaleRadio })
                                        .start();
                                    const firstbgbackNode = this._firstCardNode.getChildByName('bgback');
                                    cc.tween(this._firstCardNode)
                                        .to(0.3, { scaleX: 0 })
                                        .call(() => {
                                            firstbgbackNode.opacity = 1000;
                                            // labelNode.opacity = 0;
                                            // var playAudio = firstbgbackNode.getComponent(cc.AudioSource);
                                            var flipButton = firstbgbackNode.getComponent(cc.Button);
                                            flipButton.enabled = true;
                                            // playAudio.play();

                                        })
                                        .to(0.3, { scaleX: scaleRadio })
                                        .start();
                                }
                                this._clickItemIdx = null;
                                this._clickItem2Idx = null;
                                // this._firstCardNode = null;

                            }
                            return;
                        }
                        cc.assetManager.loadRemote(audioUrl, (err, audioClip) => {
                            const audioId = cc.audioEngine.play(audioClip, false, 0.8);
                            cc.audioEngine.setFinishCallback(audioId, () => {
                                if (!this._clickItemIdx) {
                                    this._clickItemIdx = this._imageResList[index].cardId;
                                    this._firstCardNode = cardNode;

                                } else {
                                    this._clickItem2Idx = this._imageResList[index].cardId;

                                    if (this._clickItemIdx == this._clickItem2Idx) {
                                        const dragonNode = cardNode.getChildByName('dragon');
                                        let dragonAnimation = dragonNode.getComponent(dragonBones.ArmatureDisplay);
                                        dragonNode.opacity = 1000;
                                        dragonAnimation.playAnimation('', 1);

                                        const dragonNode2 = this._firstCardNode.getChildByName('dragon');
                                        let dragonAnimation2 = dragonNode2.getComponent(dragonBones.ArmatureDisplay);
                                        dragonNode2.opacity = 1000;
                                        dragonAnimation2.playAnimation('', 1);


                                        var playAudio = cardNode.getChildByName('rightsound').getComponent(cc.AudioSource);
                                        playAudio.play();
                                        //配对，无操作，播动画
                                        const copyNode = this._firstCardNode;
                                        this.scheduleOnce(() => {
                                            cardNode.opacity = 0;
                                            copyNode.opacity = 0;
                                            this._flipSuccessCount += 2;
                                            if (this._flipSuccessCount == this._imageResList.length) {
                                                console.log('游戏完成finish！！！！！！！！！');
                                                var playAudio = cc.find('Canvas/successsound').getComponent(cc.AudioSource);
                                                playAudio.play();
                                                showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(0, -400), cc.v2(0, 1000), 200, 200);
                                                showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(-600, -400), cc.v2(200, 1000), 200, 200);
                                                showFireworks(cc.find('Canvas'), cc.find('finishani').children, cc.v2(600, -400), cc.v2(-200, 1000), 200, 200);

                                                const replayBtnNode = cc.find('Canvas/restartButton');
                                                replayBtnNode.opacity = 255;
                                                const playAudio2 = replayBtnNode.getChildByName('sound').getComponent(cc.AudioSource);
                                                const replayBtn = replayBtnNode.getChildByName('button');
                                                replayBtnNode.zIndex = 1111;
                                                replayBtn.on('click', () => {
                                                    playAudio2.play();
                                                    console.log('0000000000');
                                                    cc.game.restart();
                                                });
                                                layoutNode.active = false;
                                            }
                                        }, 0.5);



                                    } else {
                                        var playAudio = cardNode.getChildByName('wrongsound').getComponent(cc.AudioSource);
                                        playAudio.play();
                                        //错误，重置两个卡片
                                        cc.tween(cardNode)
                                            .to(0.3, { scaleX: 0 })
                                            .call(() => {
                                                bgbackNode.opacity = 1000;
                                                // labelNode.opacity = 0;
                                                // var playAudio = bgbackNode.getComponent(cc.AudioSource);
                                                var flipButton = bgbackNode.getComponent(cc.Button);
                                                flipButton.enabled = true;
                                                // playAudio.play();
                                            })
                                            .to(0.3, { scaleX: scaleRadio })
                                            .start();
                                        const firstbgbackNode = this._firstCardNode.getChildByName('bgback');
                                        cc.tween(this._firstCardNode)
                                            .to(0.3, { scaleX: 0 })
                                            .call(() => {
                                                firstbgbackNode.opacity = 1000;
                                                // labelNode.opacity = 0;
                                                // var playAudio = firstbgbackNode.getComponent(cc.AudioSource);
                                                var flipButton = firstbgbackNode.getComponent(cc.Button);
                                                flipButton.enabled = true;
                                                // playAudio.play();

                                            })
                                            .to(0.3, { scaleX: scaleRadio })
                                            .start();
                                    }
                                    this._clickItemIdx = null;
                                    this._clickItem2Idx = null;
                                    // this._firstCardNode = null;

                                }
                            });
                        });


                    })
                    .start();
            }, this);
        }

    }

    // update (dt) {},
});
