/**
 * @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};
    chrome.app.window.create('windows/popup.html', {
        id:'popup',
        resizable:false,
        alwaysOnTop:true,
        hidden:true,
        frame:{type:'none'}
    });
    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 popup = chrome.app.window.get('popup');
    var popWidth = 300,popHeight = 400;
    currentWord = word;
    chrome.system.display.getInfo(function(display){
        console.log(123)
        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 deleteWord(word){
    wordDataArr.splice(wordDataArr.indexOf(word),1);
    wordDataObj[word] = null;
    chrome.runtime.sendMessage('updateList');
    if(wordStarArr.indexOf(word)>=0){
        wordStarArr.splice(wordStarArr.indexOf(word),1);
        chrome.runtime.sendMessage('updateStarList');
    }
}

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

function importWord(wordArr){
    var word,wordCount = 0;;
    if(wordArr.length>0){
        for(var i = 0;i<wordArr.length;i++){
            word = (wordArr[i]).toLowerCase();
            if(word=='') continue;
            if(!wordDataObj[word]){
                wordCount++;
                wordDataObj[word] = {testCount:0}
                wordDataArr.push(word);
            }
            // wordsObj[word] = {testCount:0}
        };
        if(wordCount>0) wordDataArr.sort();
    }
    chrome.runtime.sendMessage('updateList');
    chrome.storage.local.set({wordsArr:wordDataArr,wordsObj:wordDataObj},function(e){
        chrome.notifications.create('importNotifi'+(new Date().getTime()),{iconUrl:'./asset/img/logo@x128.png',message:'词库导入成功\n本次共导入['+wordCount+']个新单词',type:'basic',title:'导入完成!'});
        $('.loading').removeClass('on');
        // console.log('导入完成',e);
    })
}

function clearDatas(){
    wordDataArr = [];
    wordStarArr = [];
    wordDataObj = {};
    wordOpt     = {};
    chrome.runtime.sendMessage('updateStarList');
    chrome.storage.local.set({wordsArr:wordDataArr,wordsObj:wordDataObj,wordOpt:wordOpt,starsArr:wordStarArr},function(e){
        chrome.notifications.create('clearDone',{iconUrl:'./asset/img/logo@x128.png',message:'数据清除成功',type:'basic',title:'清除完成!'});
    })
}

