
import i18n from "../i18n/i18n";
import ViewManage, { AudioMgr, GameViewType } from "./ViewManage";
import Player, { UserData } from "../game/data/udata/Player";
import SManage, { RewardData } from "../game/data/sdata/SManage";
import CHttp, { ReveData } from "../util/CHttp";
import Profile from "../util/Profile";
import ViewObject from "./ViewObject";
import Tips from "../util/Tips";
import RewardView from "../game/common/RewardView";
import Home from "../game/home/Home";
import GuideMask from "../plot/GuideMask";
import AdminLogin from "../login/AdminLogin";
/**
 * 主界面
 */
const { ccclass, property } = cc._decorator;

@ccclass
export default class Main extends cc.Component {
    /**
     * view管理
     */
    public viewManage: ViewManage;

    //跟随角色摄像机
    @property(cc.Camera)
    mMapCamera: cc.Camera = null;

    @property(cc.Node)
    mLoading: cc.Node = null;

    @property(cc.Prefab)
    mTips: cc.Prefab = null;

    @property(GuideMask)
    mGuideMask: GuideMask = null;//最上层的引导

    @property(i18n)
    mI18n: i18n = null;

    @property(cc.SpriteFrame)
    RED_POINT: cc.SpriteFrame = null;

    @property(cc.Node)
    topNode: cc.Node = null;
    /**
     * 主页
     */
    public home: Home;
    /**
     * 系统数据管理
     */
    public sManage: SManage;

    /**
     * 用户数据
     */
    public userData: UserData;
    /**
     * 玩家数据
     */
    public player: Player;
    /**
     * 配置文件
     */
    public profile: Profile = null;
    /**
     * 网络连接
     */
    public loginHttp: CHttp;

    /**
     * 游戏网络连接
     */
    public gameHttp: CHttp;



    onLoad() {
        // cc.view.enableAntiAlias(false);
        // cc.game.setFrameRate(35);
        // cc.director.getPhysicsManager().enabledAccumulator=true;
        // cc.PhysicsManager.FIXED_TIME_STEP = 1/35
        // cc.director.getCollisionManager().enabled = true;
        // cc.director.getCollisionManager().enabledDebugDraw = true;

        // cc.director.getPhysicsManager().enabled = true;
        // cc.director.getPhysicsManager().debugDrawFlags = 1;

        this.viewManage = this.node.getComponent(ViewManage);
        cc.view.resizeWithBrowserSize(true);
        this.resizeView();

        cc.view.setResizeCallback(() => {
            cc.log('view resize callback');
            this.resizeView();
        });
        this.profile = this.node.getComponent(Profile);
        this.loginHttp = new CHttp(this.profile.url);
    }
    start() {
        this.mI18n.__init(() => {
            // cc.log('语言测试 hello:', i18n.t('label_text.hello',{name: 'chelios'}));
            cc.log('语言测试 rabbit:', i18n.t('rabbit'));
            this.toHotUpdate();
        });
    }
    private resizeView() {
        let winsize = cc.winSize;
        let dt = winsize.width / winsize.height;
        if (dt > 1.78) {
            cc.Canvas.instance.fitWidth = false;
            cc.Canvas.instance.fitHeight = true;
        } else {
            cc.Canvas.instance.fitWidth = true;
            cc.Canvas.instance.fitHeight = false;
        }
        cc.log('屏幕尺寸:', winsize.width, winsize.height);
        cc.log('canvas尺寸:', cc.Canvas.instance.node.width, cc.Canvas.instance.node.height);
        cc.log('屏幕比例:', dt);
    }
    /**
     * 获取多国语言
     */
    public get i18n() {
        return i18n;
    }
    public startLoad() {
        this.mLoading.active = true;
    }
    public stopLoad() {
        this.mLoading.active = false;
    }
    public showTips(msg) {
        let node = cc.instantiate(this.mTips);
        let tips = node.getComponent(Tips);
        tips.setLabel(i18n.t(msg))
        let winsize = cc.winSize;
        node.y += winsize.height / 5;
        node.parent = this.node;
    }
    public showTips1(reve: ReveData) {
        let list: Array<RewardData> = this.sManage.getRewards(reve)
        if (list.length <= 0) {
            return
        }
        let str = '<color=#FFFFFF>' + i18n.t('获得道具') + ':</c><color=#0fffff>'
        for (let i = 0; i < list.length; i++) {
            const element = list[i];
            str += element.name + 'x' + element.count + ';'
        }
        str += '</c>'
        this.showTips(str)
    }
    /**
     * 显示从服务器上获取的奖励
     * @param msg 
     */
    public showReward(reve: ReveData, callback?: () => void, title?: string, about?: string) {
        let list = this.sManage.getRewards(reve)
        this.showRewardList(list, callback, title, about)
    }
    public showRewardList(list, callback?: () => void, title?: string, about?: string) {
        if (list.length > 0) {
            this.viewManage.loadFunc(GameViewType.reward, (viewObject: ViewObject) => {
                let rewardView = viewObject as RewardView;
                rewardView.init(list)
                if (title) {
                    rewardView.mTitle.string = i18n.t(title)
                }
                if (about) {
                    rewardView.mAbout.string = i18n.t(about)
                } else {
                    rewardView.mAbout.string = ''
                }
                if (callback) {
                    rewardView.setCloseCallback(() => {
                        callback()
                    })
                }
                viewObject.show();
                this.playerEffectByPath(AudioMgr.award);
            });
        } else {
            if (callback) {
                callback()
            }
        }
    }
    /**
     * 进入热更界面
     */
    public toHotUpdate() {
        // if(AdminLogin.userData){
        //     this.userData = AdminLogin.userData
        //     this.viewManage.loadFunc(GameViewType.area,(viewObject:ViewObject)=>{
        //         viewObject.show();
        //     });
        //     return
        // }
        // if (cc.sys.isNative) {
        //     //FqLogin
        //     this.viewManage.loadFunc(GameViewType.fqLogin, (viewObject: ViewObject) => {
        //         viewObject.show();
        //     });
        // } else {
        //     this.viewManage.loadFunc(GameViewType.login, (viewObject: ViewObject) => {
        //         viewObject.show();
        //     });
        // }

        this.viewManage.loadFunc(GameViewType.login,(viewObject:ViewObject)=>{
            viewObject.show();
        });

        // if(CC_DEBUG){
        //     //载入测试的登陆界面
        //     this.viewManage.loadFunc(GameViewType.login,(viewObject:ViewObject)=>{
        //         viewObject.show();
        //     });
        // }else{
        //     //只有isNative 版本才需要进行热更新
        //     if (cc.sys.isNative) {

        //     } else {
        //     }
        // }
    }
    // public mp3Path = null;
    // public mp3Clip:cc.AudioClip = null;
    // public backSoundId = -1;
    // public playMusic() {
    //     if(this.mp3Clip){
    //         this.playSound1Clip(this.mp3Clip);
    //     }else{
    //         this.playSound1(this.mp3Path);
    //     }

    // }
    // public pauseMusic() {
    //     cc.audioEngine.stopAll();
    // }
    // /**
    //  * 播放背景音乐
    //  * @param path 
    //  */
    // public playSound1(path:string){
    //     this.mp3Path = path;
    //     this.mp3Clip.destroy();
    //     this.mp3Clip = null;
    //     if(path == null){
    //         return;
    //     }
    //     let setting = this.player.setting;
    //     cc.audioEngine.stopAll();
    //     this.backSoundId = -1;
    //     if(setting.music_ON_OFF_1){
    //         return;
    //     }else{
    //         cc.resources.load(path,cc.AudioClip, (err, mp3:cc.AudioClip)=>{
    //             if(err){
    //                 cc.log(err);
    //             }else{
    //                 if(setting.music_ON_OFF_1){
    //                     this.backSoundId = cc.audioEngine.play(mp3, true, setting.music_Slider_1);
    //                 }
    //             }
    //         });
    //     }
    // }
    // public playSound1Clip(mp3:cc.AudioClip){
    //     this.mp3Path = null;
    //     this.mp3Clip = mp3;
    //     cc.audioEngine.stopAll();
    //     let setting = this.player.setting;
    //     if(setting.music_ON_OFF_1){
    //         this.backSoundId = cc.audioEngine.play(mp3, true, setting.music_Slider_1);
    //     }
    // }
    // /**
    //  * 播放特效音乐
    //  * @param path 
    //  */
    // public playSound2(path:string){
    //     let setting = this.player.setting;
    //     if(setting.music_ON_OFF_2){
    //         cc.loader.loadRes(path,cc.AudioClip, (err, mp3)=>{
    //             if(err){
    //                 cc.log(err);
    //             }else{
    //                 if(setting.music_ON_OFF_2){
    //                     cc.audioEngine.play(mp3, false, setting.music_Slider_2);
    //                 }
    //             }
    //         });
    //     }
    // }
    // public playSound2Clip(mp3:cc.AudioClip){
    //     let setting = this.player.setting;
    //     if(setting.music_ON_OFF_2){
    //         cc.audioEngine.play(mp3, false, setting.music_Slider_2);
    //     }
    // }


    public bgmId: number = -1;
    public effectId: number = -1;
    public effectList: number[] = [];

    // 播放背景音乐
    playMusicByClip(clip: cc.AudioClip) {
        let setting = this.player.setting;
        if (this.bgmId != -1) {
            cc.audioEngine.stopMusic();
            this.bgmId = -1;
        }
        if (setting.music_ON_OFF_1) {
            cc.audioEngine.setMusicVolume(setting.music_Slider_1);
        } else {
            cc.audioEngine.setMusicVolume(0);
        }
        this.bgmId = cc.audioEngine.playMusic(clip, true);
    }

    // 播放背景音乐
    playMusicByPath(path: string) {
        if (path == "") return
        cc.resources.load(path, cc.AudioClip, (err, mp3: cc.AudioClip) => {
            if (err) {
                console.log("===播放背景音乐 err===", err)
            } else {
                this.playMusicByClip(mp3);
            }
        });
    }

    // 播放音效
    playerEffectByClip(clip: cc.AudioClip) {
        let setting = this.player.setting;
        if (setting.music_ON_OFF_2) {
            if (this.effectId != -1) {
                cc.audioEngine.stopEffect(this.effectId);
                this.effectId = -1;
            }
            this.effectId = cc.audioEngine.playEffect(clip, false);
            cc.audioEngine.setEffectsVolume(setting.music_Slider_2);
        }
    }

    // 播放音效
    playerEffectByPath(path: string) {
        if (path == "") return
        let setting = this.player.setting;
        if (setting.music_ON_OFF_2) {
            cc.resources.load(path, cc.AudioClip, (err, mp3: cc.AudioClip) => {
                if (err) {
                    console.log("===播放背景音效 err===", err)
                } else {
                    this.playerEffectByClip(mp3);
                }
            });
        }
    }

    // 暂停音乐
    public pauseMusic() {
        let setting = this.player.setting;
        if (!setting.music_ON_OFF_1) {
            if (this.bgmId != -1) {
                cc.audioEngine.pauseMusic();
            }
        }
    }

    //继续播放音乐
    public resumeMusic() {
        let setting = this.player.setting;
        if (setting.music_ON_OFF_1) {
            if (this.bgmId != -1) {
                cc.audioEngine.resumeMusic();
                cc.audioEngine.setMusicVolume(setting.music_Slider_1);
            }
        }
    }

    public stopAll() {
        this.effectList = [];
        cc.audioEngine.stopAll();
    }
}
