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

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,

        dividerBg: cc.Node,

        _defaultY: 469,
        _selectedY: 477,

    },

    onLoad() {
        this.friendTabBgs = ['./textures/friendlist/tab_friend_selected', './textures/friendlist/tab_friend_default'];
        this.recommendTabBgs = ['./textures/friendlist/tab_recommend_selected', './textures/friendlist/tab_recommend_default']
        this.wechatFriendTabBgs = ['./textures/friendlist/tab_wechat_friend_selected', './textures/friendlist/tab_wechat_friend_default'];

        this.dividerBg.zIndex = 2;

        this.node.on('touchstart', () => { }, this);
    },

    onEnable() {
        console.log('friendSystem onEnable');


    },

    onDisable() {
        console.log('friendSystem onDisable');

    },

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

        this.content.y = -cc.view.getVisibleSize().height;


        this.scheduleOnce(() => {
            let s = cc.sequence(cc.moveTo(0.2, 0, -40).easing(cc.easeCubicActionOut()), cc.moveBy(0.05, 0, -20));
            this.content.runAction(s);
        }, 0.1);
    },

    close() {
        if (this.node && this.node.parent) {
            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;
    },

    closeFriendSystem: function () {
        this.currentIndex = -1;
        this.node.active = false;
        this._wechatFriendNode.active = false;
    },

    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);
            } 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];
            let moveY = selected ? this._selectedY : this._defaultY;
            DWTool.loadResSpriteFrame(bg).then((spriteFrame) => {
                button.getComponent(cc.Button).normalSprite = spriteFrame;
            });
            listViewNode.active = selected;

            // button.node.zIndex = selected ? 1 : 0;
            // button.node.y = moveY;

            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) {},
});