
var WeChat = require("../net/WeChat");
const DWTool = require('../utils/DWTool');
var shareDialog = require('./ShareDialog')


cc.Class({
    extends: cc.Component,

    properties: {

        content: cc.Node,

        friendNode: cc.Node,

        recommendNode: cc.Node,

        _wechatFriendNode: cc.Node,

        friendButton: cc.Button,
        recommendButton: cc.Button,
        wechatFriendButton: cc.Button,



    },

    onLoad() {
        this.node.height = cc.view.getVisibleSize().height;
        this.contentY = this.content.y = (this.content.height - this.node.height) / 2;

        this.friendTabBgs = ['./friendList/tab_friend_selected', './friendList/tab_friend_default'];
        this.recommendTabBgs = ['./friendList/tab_recommend_selected', './friendList/tab_recommend_default'];
        this.wechatFriendTabBgs = ['./friendList/tab_wechat_friend_selected', './friendList/tab_wechat_friend_default'];

        this._defaultY = 496;
        this._selectedY = 480;

    },

    onEnable() {

    },

    onDisable() {

    },

    show(wechatFriendNode) {
        this.setWechatFriendNode(wechatFriendNode);
        this.node.parent = cc.find('Canvas');
        this.node.active = true;

        this.content.y = -cc.view.getVisibleSize().height;
        let s = cc.sequence(cc.moveTo(0.2, 0, this.contentY + 20).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
        this.content.runAction(s);
        this.scheduleOnce(() => {
            this.setTabState(0);
        }, 0.2);
    },

    close() {
        if (this.node && this.node.parent) {
            this._wechatFriendNode.active = false;
            let finish = cc.callFunc(() => {
                this.closeFriendSystem();
            }, this)
            this.content.runAction(cc.sequence(cc.moveTo(0.2, 0, -cc.view.getVisibleSize().height).easing(cc.easeCubicActionIn()), finish));
        }
    },

    setTabState(currentIndex) {
        if (this.currentIndex === currentIndex) {
            return;
        }
        console.log('currentIndex:' + currentIndex);
        this.currentIndex = currentIndex;
        switch (currentIndex) {
            case 0:
                this.setTabSelected(true, this.friendButton, this.friendTabBgs, this.friendNode);
                this.setTabSelected(false, this.recommendButton, this.recommendTabBgs, this.recommendNode);
                this.setTabSelected(false, this.wechatFriendButton, this.wechatFriendTabBgs, this._wechatFriendNode);
                break;
            case 1:
                this.setTabSelected(false, this.friendButton, this.friendTabBgs, this.friendNode);
                this.setTabSelected(true, this.recommendButton, this.recommendTabBgs, this.recommendNode);
                this.setTabSelected(false, this.wechatFriendButton, this.wechatFriendTabBgs, this._wechatFriendNode);
                break;
            case 2:
                this._wechatFriendNode.zIndex = 2;
                this.setTabSelected(false, this.friendButton, this.friendTabBgs, this.friendNode);
                this.setTabSelected(false, this.recommendButton, this.recommendTabBgs, this.recommendNode);
                this.setTabSelected(true, this.wechatFriendButton, this.wechatFriendTabBgs, this._wechatFriendNode);
                break;
        }
    },

    setWechatFriendNode(wechatFriendNode) {
        this._wechatFriendNode = wechatFriendNode;
        this._wechatFriendNode.height = cc.view.getVisibleSize().height;
    },

    closeFriendSystem: function () {
        this.friendNode.getComponent('FriendList').friendLastRequestTime = -1;
        this.currentIndex = -1;
        this.node.active = false;
        this._wechatFriendNode.active = false;
    },

    showTalent() {
        cc.loader.loadRes('/prefabs/share_dialog', cc.Prefab, (error, prefab) => {
            if (error === null) {
                let shareDialog = cc.instantiate(prefab);
                this.node.addChild(shareDialog);
                shareDialog.getComponent('ShareDialog').showTalent();
            } else {
                console.log(JSON.stringify(error));
            }
        });
    },

    inviteFriend: function () {
        // WeChat.inviteFriend();
        cc.loader.loadRes('/prefabs/share_dialog', cc.Prefab, (error, prefab) => {
            if (error === null) {
                let shareDialog = cc.instantiate(prefab);
                this.node.addChild(shareDialog);
                shareDialog.getComponent('ShareDialog').showInviteFriend();
            } else {
                console.log(JSON.stringify(error));
            }
        });
    },

    changeToWechatFriendTab() {
        this.setTabState(2);
    },

    changeToRecommendTab() {
        this.setTabState(1);
    },

    changeToFriendTab() {
        this.setTabState(0);
    },

    setTabSelected(selected, button, tabBgs, listViewNode) {
        if (selected && button.node.zIndex === 0 || !selected && button.node.zIndex === 1) {
            let bg = selected ? tabBgs[0] : tabBgs[1];
            DWTool.loadResSpriteFrame(bg).then((spriteFrame) => {
                button.getComponent(cc.Sprite).spriteFrame = spriteFrame;
            });
            listViewNode.active = selected;
            button.node.zIndex = selected ? 1 : 0;
            button.node.y = selected ? this._selectedY : this._defaultY;

            // let finish = cc.callFunc(() => {
            //     button.node.zIndex = selected ? 1 : 0;
            // })

            // var animation = cc.sequence(cc.moveTo(0.2, button.node.x, moveY), finish);
            // button.node.runAction(animation);
        }
    },
    // update (dt) {},
});