
const DWTool = require('./utils/DWTool');
const Style = require('./utils/GameEnum').LevelHomeFriendItemStyle;
const HomeApi = require('./net/HomeApi');
const GameModule = require("./utils/GameModule");
const Notikey = require('./utils/GameEnum').GameNotificationKey;

cc.Class({
    extends: cc.Component,

    properties: {

        bgNode: cc.Node,
     
        friendSpriteFrames: [cc.SpriteFrame],

        artistSpriteFrames: [cc.SpriteFrame],

        friendBtn: cc.Button,

        artistBtn: cc.Button,

        friendItem: cc.Prefab,

        friendScrollView: cc.ScrollView,

        artistScrollView: cc.ScrollView,

        noArtistTipNode: cc.Node,

        _currentTab: 0 

    },

    // LIFE-CYCLE CALLBACKS:
    onLoad () {

        GameModule.tab = this.node;

        this.friends = [];
        this.artists = [];
        this.setBtnSpriteFrames();

        this.bgNode.zIndex = 1;
        this.friendScrollView.node.zIndex = 1;
        this.artistScrollView.node.zIndex = 1;

        if (this._currentTab === 0) {
            this.friendBtn.node.setScale(1.0, 1.0);    
            this.artistBtn.node.setScale(0.9, 0.9);  
            this.friendBtn.node.zIndex = 2;            
            this.artistBtn.node.zIndex = 0;       
            this.friendScrollView.node.active = true;   
            this.artistScrollView.node.active = false;
            this.artistScrollView.node.position = cc.v2(0, -250);
        } else {
            this.friendBtn.node.setScale(0.9, 0.9);    
            this.artistBtn.node.setScale(1.0, 1.0);  
            this.friendBtn.node.zIndex = 0;          
            this.artistBtn.node.zIndex = 2;
            this.friendScrollView.node.active = false;
            this.artistScrollView.node.active = true;
            this.friendScrollView.node.position = cc.v2(0, -250);
        }
        
        this.loadFriendData()
        .then((result) => { // 好友数据回调
            this.friends = result || [];
            return this.loadArtists();
        }).then((result) => {  // 艺人数据回调
            if (result != undefined) {
                this.artists = result;
                this.noArtistTipNode.active = false;
            } else {
                this.noArtistTipNode.active = true;
            }
            this.layout();
        }).catch((err) => { // 捕获到报错
            console.log(err);
        });
        

        // 刷新好友列表
        GameEvent.on(Notikey.RefreshFriendList, this, () => {
            
            this.loadFriendData()
            .then((result) => {
                this.friends = result || [];  
                this.friendLayout();
            }).catch((err) => {
                console.log(err); 
            });
        });

        // 刷新艺人列表
        GameEvent.on(Notikey.RefreshArtistManagerList, this, () => {

            this.loadArtists()
            .then((result) => {
                if (result != undefined) { 
                    this.artists = result;
                    this.noArtistTipNode.active = false;
                } else {
                    this.artists = [];
                    this.noArtistTipNode.active = true;
                }
                   
                this.artistLayout(); 
            }).catch((err) => {
                console.log(err); 
            });
        });

        this.changeTabEvent = _.debounce((eventData) => {
            this._currentTab = eventData;
            this.setBtnSpriteFrames();
    
            let showBtn = (eventData == 0) ? this.friendBtn : this.artistBtn;
            let hideBtn = (eventData == 0) ? this.artistBtn : this.friendBtn;

            let showScrollView = (eventData == 0) ? this.friendScrollView : this.artistScrollView;
            let hideScrollView = (eventData == 0) ? this.artistScrollView : this.friendScrollView;

            this.showAnimation(showBtn, hideScrollView);
            this.hideAnimation(hideBtn, showScrollView);
        }, 500, true);
    },

    setBtnSpriteFrames() {
        this.friendBtn.getComponent(cc.Sprite).spriteFrame = this.friendSpriteFrames[this._currentTab];
        this.artistBtn.getComponent(cc.Sprite).spriteFrame = this.artistSpriteFrames[this._currentTab];
    },

    showAnimation(btn, scrollView) {
        let tempBtn = btn;
        let tempScrollView = scrollView;
        tempBtn.node.zIndex = 2;
        let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -250));
        let scrollCallback = cc.callFunc(() => {
            tempScrollView.node.active = false;
        });
        tempScrollView.node.runAction(cc.sequence(scrollMoveAction, scrollCallback));
        
    },

    hideAnimation(btn, scrollView) {
        let tempBtn = btn;
        let tempScrollView = scrollView;
        tempBtn.node.zIndex = 0;
        tempScrollView.node.active = true;
        let scrollMoveAction = cc.moveTo(0.3, cc.p(0, -10));
        tempScrollView.node.runAction(scrollMoveAction);
    },

    changeTab(target, eventData) {

        if (this._currentTab == eventData) { return; }
        
        this.changeTabEvent(eventData);
    },

    loadFriendData() {
        return new Promise((resolve, reject) => {
            HomeApi.getFriends((responseData) => {
                resolve(responseData);
            }, (error) => {
                reject(error);
            });
        });
    },

    loadArtists() {
        return new Promise((resolve, reject) => {
            HomeApi.getFriendManageList( (responseData) => {
                resolve(responseData);
            }, (error) => {
                reject(error);
            })
        });       
    },

    layout() {
        
        this.friendLayout();

        this.artistLayout();      
    },

    friendLayout() {

        for (let child of this.friendScrollView.content.children) {
            child.destroy();
        }
        
        // 默认有全部好友和邀请两个节点
        let count = 1;
        count += this.friends.length;

        for (let i = 0; i < count; i++) {
            let item = cc.instantiate(this.friendItem);

            let scriptComponent = item.getComponent('LevelHomeFriendItem');
            if (i === 0) {
                scriptComponent.style = Style.AllFriend;
            } else {
                scriptComponent.style = Style.Friend; 
                scriptComponent.setFriendInfo(this.friends[i-1]);
            }
            this.friendScrollView.content.addChild(item);
        }
    },

    artistLayout() {

        for (let child of this.artistScrollView.content.children) {
            child.destroy();
        }

        let artistCount = 1;
        artistCount += this.artists.length;

        for (let i = 0; i < artistCount; i++) {
            let item = cc.instantiate(this.friendItem);
            
            let scriptComponent = item.getComponent('LevelHomeFriendItem');
            if (i === 0) {
                scriptComponent.style = Style.Talent; 
            } else {
                scriptComponent.style = Style.Artist; 
                scriptComponent.setFriendInfo(this.artists[i-1]);
            }
            this.artistScrollView.content.addChild(item);
        }
    },

});
