/**
 * @Author  HonorLee (dev@honorlee.me)
 * @Version 1.0 (2019-04-21)
 * @License MIT
 */
var mainWidth = 600,mainHeight = 800;
var wordDataArr,wordStarArr,wordDataObj,wordOpt,currentWord,setting;
var autoShowTimer;
chrome.storage.local.get(['wordsArr','starsArr','wordsObj','wordOpt','setting'],function(items){
    wordDataArr = items.wordsArr?items.wordsArr:[];
    wordStarArr = items.starsArr?items.starsArr:[];
    wordDataObj = items.wordsObj?items.wordsObj:{};
    wordOpt     = items.wordOpt?items.wordOpt:{};
    setting     = items.setting?items.setting:{timerMin:10};
    setTimer();
});
chrome.app.runtime.onLaunched.addListener(function() {
    chrome.app.window.create('windows/main.html', {
        id:'main',
        innerBounds:{width:mainWidth,height:mainHeight},
        resizable:false,
        frame:{type:'none'}
    },function(mainWindow){
        mainWindow.resizeTo(mainWidth,mainHeight);
        mainWindow.onClosed.addListener(function(){
            chrome.storage.local.set({wordsArr:wordDataArr,starsArr:wordStarArr,wordsObj:wordDataObj,wordOpt:wordOpt,setting:setting});
        });
    });
});
var showWordInfo = function(word){
    var oldWindow = chrome.app.window.get('popup');
    if(oldWindow){
        createPopup(oldWindow,word);
    }else{
        chrome.app.window.create('windows/popup.html', {
            id:'popup',
            resizable:false,
            alwaysOnTop:true,
            hidden:true,
            frame:{type:'none'}
        },function(popup){
            createPopup(popup,word);
        });
    }
}

function createPopup(popup,word){
    var popWidth = 300,popHeight = 400;
    currentWord = word;
    chrome.system.display.getInfo(function(display){
        var displayOpt = display[0].workArea;
        popup.outerBounds.left = displayOpt.width - popWidth - 20;
        popup.outerBounds.top = 20 + displayOpt.top;
        // popup.innerBounds = {width:popWidth,height:popHeight};
        popup.show();
        chrome.runtime.sendMessage('updateInfo');
        // popup.onClosed.addListener(function(){
        //     chrome.storage.local.set({wordsObj:bg.wordDataObj,wordOpt:bg.wordOpt});
        // });
    });
}

function randomShow(){
    console.log(1)
    if(wordDataArr.length==0) return;
    var mid = 3;
    var percent = Math.floor(Math.random()*10);
    var randomIndex = 0,word;
    if(wordStarArr.length==0) percent = 0;
    if(percent<=mid){
        randomIndex = Math.floor(Math.random()*wordDataArr.length);
        word = wordDataArr[randomIndex];
    }else{
        randomIndex = Math.floor(Math.random()*wordStarArr.length);
        word = wordStarArr[randomIndex];
    }
    showWordInfo(word);
}
//0:unstar 1:star
function starWord(type,word){
    if(type==0){
        var wi = wordStarArr.indexOf(word);
        wordStarArr.splice(wi,1);
    }else if(type==1){
        wordStarArr.push(word);
    }
    wordDataObj[word]['isStar'] = type;
    wordStarArr.sort();
    chrome.storage.local.set({starsArr:wordStarArr,wordsObj:wordDataObj},function(e){
        chrome.notifications.create('importNotifi'+(new Date().getTime()),{iconUrl:'../asset/img/logo@x128.png',message:'单词['+word+']已'+(type==0?'从收藏列表移除':'加入收藏'),type:'basic',title:'设置已保存!'});
        chrome.runtime.sendMessage('updateStarList');
    })
}

function setTimer(){
    if(autoShowTimer) clearInterval(autoShowTimer);
    autoShowTimer = setInterval(randomShow, setting.timerMin*1000*60);
}




