//-----------------------------------------------------------------------------
// Scene_Title
//
// The scene class of the title screen.

function Scene_Title() {
    this.initialize(...arguments);
}

Scene_Title.prototype = Object.create(Scene_Base.prototype);
Scene_Title.prototype.constructor = Scene_Title;

Scene_Title.prototype.initialize = function() {
    Scene_Base.prototype.initialize.call(this);
    this._decide = false;
    this.setIndex();
};

Scene_Title.prototype.setIndex = function() {
    if(this.isContinueEnabled()){
        this._index = 1;
    }else{
        this._index = 0;
    }
};

Scene_Title.prototype.isContinueEnabled = function() {
    return DataManager.isAnySavefileExists();
};

Scene_Title.prototype.create = function() {
    Scene_Base.prototype.create.call(this);
    this.createBackground();
    this.createForeground();
    //this.createWindowLayer();
    //this.createCommandWindow();
    this.createCommandButtons();
    this.createOptionButton();
    this.createAchiveButton();

    this.createPrecaution();//test
    this.setLanguage();//test
};

Scene_Title.prototype.start = function() {
    Scene_Base.prototype.start.call(this);
    SceneManager.clearStack();
    this.adjustBackground();
    this.playTitleMusic();
    this.startFadeIn(this.fadeSpeed(), false);
};

Scene_Title.prototype.update = function() {
    Scene_Base.prototype.update.call(this);
    this.updateEffects();
    this.updateInput();
};

Scene_Title.prototype.isBusy = function() {
    return (
        Scene_Base.prototype.isBusy.call(this)
    );
};

Scene_Title.prototype.terminate = function() {
    Scene_Base.prototype.terminate.call(this);
    SceneManager.snapForBackground();
    if (this._gameTitleSprite) {
        this._gameTitleSprite.bitmap.destroy();
    }
};

Scene_Title.prototype.createBackground = function() {
    this._backSprite1 = new Sprite(
        ImageManager.loadTitle1($dataSystem.title1Name)
    );
    this._backSprite2 = new Sprite(
        ImageManager.loadTitle2($dataSystem.title2Name)
    );
    this.addChild(this._backSprite1);
    this.addChild(this._backSprite2);
};

Scene_Title.prototype.createForeground = function() {
    this._gameTitleSprite = new Sprite(
        new Bitmap(Graphics.width, Graphics.height)
    );
    this.addChild(this._gameTitleSprite);
    if ($dataSystem.optDrawTitle) {
        this.drawGameTitle();
    }
};

Scene_Title.prototype.drawGameTitle = function() {
    const x = 20;
    const y = Graphics.height / 4;
    const maxWidth = Graphics.width - x * 2;
    const text = $dataSystem.gameTitle;
    const bitmap = this._gameTitleSprite.bitmap;
    bitmap.fontFace = $gameSystem.mainFontFace();
    bitmap.outlineColor = "black";
    bitmap.outlineWidth = 8;
    bitmap.fontSize = 72;
    bitmap.drawText(text, x, y, maxWidth, 48, "center");
};

Scene_Title.prototype.adjustBackground = function() {
    this.scaleSprite(this._backSprite1);
    this.scaleSprite(this._backSprite2);
    this.centerSprite(this._backSprite1);
    this.centerSprite(this._backSprite2);
};

Scene_Title.prototype.createCommandButtons = function() {
    const x = Graphics.boxWidth / 2;
    const y = Graphics.boxHeight - 150;
    this._buttons = [];
    this._effects = [];
    this.createButton(0, x, y);
    if(this.isContinueEnabled()){
        this.createButton(1, x, y + 64);
    }
    for(let i = 0; i < 2; i++){
        this.createEffect(i);
    }
    this.setEffects();
};

Scene_Title.prototype.createButton = function(i, x, y) {
    const bitmap = ImageManager.loadSystem('TitleCommand' + i);
    const buttonWidth = 180;
    const buttonHeight = 50;
    this._buttons[i] = new Sprite_ButtonEx(bitmap, x, y);
    this._buttons[i].setColdFrame(0, 0, buttonWidth, buttonHeight);
    this._buttons[i].setHotFrame(0, 0, buttonWidth, buttonHeight);
    this._buttons[i].anchor.x = 0.5;
    this._buttons[i].anchor.y = 0.5;
    this._buttons[i].setClickHandler(this.onButton.bind(this, i));
    //this._buttons[i].visible = false;
    this.addChild(this._buttons[i]);
};

Scene_Title.prototype.createOptionButton = function() {
    const bitmap = ImageManager.loadSystem('MiniButtons');
    const buttonWidth = 40;
    const buttonHeight = 40;
    const x = Graphics.boxWidth - 48;
    const y = Graphics.boxHeight - 48;
    this._optionButton = new Sprite_ButtonEx(bitmap, x, y);
    this._optionButton.setColdFrame(buttonWidth, 0, buttonWidth, buttonHeight);
    this._optionButton.setHotFrame(buttonWidth, buttonHeight, buttonWidth, buttonHeight);
    this._optionButton.setClickHandler(this.commandOptions.bind(this));
    //this._buttons[i].visible = false;
    this.addChild(this._optionButton);
};

Scene_Title.prototype.createAchiveButton = function() {
    if(!this.isContinueEnabled()) return;
    const bitmap = ImageManager.loadSystem('MiniButtons');
    const buttonWidth = 40;
    const buttonHeight = 40;
    const x = Graphics.boxWidth - 48;
    const y = 8;//Graphics.boxHeight - 48;
    this._achiveButton = new Sprite_ButtonEx(bitmap, x, y);
    this._achiveButton.setColdFrame(0, 0, buttonWidth, buttonHeight);
    this._achiveButton.setHotFrame(0, buttonHeight, buttonWidth, buttonHeight);
    this._achiveButton.setClickHandler(this.commandAchive.bind(this));
    //this._buttons[i].visible = false;
    this.addChild(this._achiveButton);
};

Scene_Title.prototype.onButton = function(i) {
    if(this._decide) return;
    SoundManager.playOk();
    this._index = i;
    this._decide = true;
    this.setEffects();
    switch(i){
        case 0 :
            this.commandNewGame();
            break;
        case 1 :
            this.commandContinue();
            break;
    }
};

Scene_Title.prototype.createEffect = function(i) {
    this._effects[i] = new Sprite();
    this._effects[i].anchor.x = 0.5;
    this._effects[i].anchor.y = 0.5;
    this._effects[i].blendMode = 1;
    this._effects[i].opacity = 0;
    this._effects[i].duration = 0;
    this.addChild(this._effects[i]);
};

Scene_Title.prototype.updateEffects = function() {
    for(let i = 0; i < this._effects.length; i++){
        this.updateEffect(i);
    }
};

Scene_Title.prototype.updateEffect = function(i) {
    const d = this._effects[i].duration;
    if(d > 0){
        const opacity = easeOut(128, 0, d, 60);
        const scale = easeOut(1.0, 1.5, d, 60);
        this._effects[i].opacity = opacity;
        this._effects[i].scale.x = scale;
        this._effects[i].scale.y = scale;
        this._effects[i].duration--;
    }
    if(d < 0){
        this._effects[i].duration++;
    }
    if(this._effects[i].duration === 0){
        this._effects[i].duration = 60;
    }
};

Scene_Title.prototype.updateInput = function() {
    if(Input.isTriggered("down")){
        this.cursorDown();
    }
    if (Input.isTriggered("up")){
        this.cursorUp();
    }
    if (Input.isTriggered("ok")){
        this.onButton(this._index);
    }
};

Scene_Title.prototype.cursorDown = function() {
    SoundManager.playCursor();
    const max = this._buttons.length - 1;
    if(this._index >= max){
        this._index = 0;
    }else{
        this._index++;
    }
    this.setEffects();
};

Scene_Title.prototype.cursorUp = function() {
    SoundManager.playCursor();
    const max = this._buttons.length - 1;
    if(this._index === 0){
        this._index = max;
    }else{
        this._index--;
    }
    this.setEffects();
};

Scene_Title.prototype.setEffects = function() {
    for(let i = 0; i < this._effects.length; i++){
        this.setEffect(i);
    }
};

Scene_Title.prototype.setEffect = function(i) {
    const index = this._index;
    const bitmap = ImageManager.loadSystem('TitleCommand' + index);
    const x = this._buttons[index].x;
    const y = this._buttons[index].y;
    this._effects[i].bitmap = bitmap;
    this._effects[i].x = x;
    this._effects[i].y = y;
    this._effects[i].duration = -10 * i;
};


Scene_Title.prototype.commandNewGame = function() {
    DataManager.setupNewGame();
    //this._commandWindow.close();
    this.fadeOutAll();
    SceneManager.goto(Scene_Map);
};

Scene_Title.prototype.commandContinue = function() {
    //this._commandWindow.close();
    SceneManager.push(Scene_Load);
};

Scene_Title.prototype.commandOptions = function() {
    //this._commandWindow.close();
    SoundManager.playOk();
    SceneManager.push(Scene_Options);
};

Scene_Title.prototype.commandAchive = function() {
    //this._commandWindow.close();
    SoundManager.playOk();
    SceneManager.push(Scene_Achive);
};

Scene_Title.prototype.playTitleMusic = function() {
    AudioManager.playBgm($dataSystem.titleBgm);
    AudioManager.stopBgs();
    AudioManager.stopMe();
};

//test
// Scene_Title.prototype.createPrecaution = function() {
//     this._infoWindow = new Window_Info();
//     this.addWindow(this._infoWindow);
// };

// function Window_Info() {
//     this.initialize(...arguments);
// };
  
// Window_Info.prototype = Object.create(Window_Base.prototype);
// Window_Info.prototype.constructor = Window_Info;

// Window_Info.prototype.initialize = function() {
//     const x = 20;//適当
//     const y = 20;
//     const width = 100;
//     const height = 100;
//     const rect = new Rectangle(x, y, width, height);

//     Window_Base.prototype.initialize.call(this, rect);
//     this.refresh();
// };

// Window_Info.prototype.refresh = function() {
//     this.contents.clear();
//     this.drawText("キャラクターは18歳以上です", 100, 100);//適当
// };

Scene_Title.prototype.createPrecaution = function() {
    this._infoWindow = new Window_Info();
    this.addChild(this._infoWindow);
};

function Window_Info() {
    this.initialize(...arguments);
};
  
Window_Info.prototype = Object.create(Window_Base.prototype);
Window_Info.prototype.constructor = Window_Info;

Window_Info.prototype.initialize = function() {
    const x = 16;//適当
    const y = 480;
    const width = 320;
    // const width = 480;
    const height = 54;
    const rect = new Rectangle(x, y, width, height);

    Window_Base.prototype.initialize.call(this, rect);
    this._isOpening = true;
    this._isWating = false;
    this._isClosing = false;
    this._duration = 30;
    this._waitTime = 120;
    this.opacity = 0;
    this.contents.paintOpacity = 0;
    this.refresh();
};

Window_Info.prototype.refresh = function() {
    const value = ConfigManager.multilingual;
	$gameVariables.setValue(25, value);
    const language = $gameVariables.value(25);
    let text;

    this.contents.clear();
    this.contents.fontSize = 14;
    switch (language) {
        case 0:
            text = "登場キャラクターは全員18歳以上です。"
            break;
        case 1:
            text = "All characters are over the age of 18."
            break;
        case 2:
            text = "这场比赛是为成年人。所有字符都超过18岁。"
            break;
        case 3:
            text = "這場比賽是為成年人。所有字符都超過18歲。"
            break;
        default:
            break;
    }
    this.drawText(text, 0, 0);//適当
};

Window_Info.prototype.update = function() {
    Window_Base.prototype.update.call(this);
    this.updateFade();
};

Window_Info.prototype.updateFade = function() {
    const d = this._duration;
    if(this._isOpening){
        const y1 = Graphics.boxHeight;
        this.y = easeOut(y1, 480, d, 30);
        this.contents.paintOpacity = easeOut(0, 255, d, 30);
        this._duration--;
        if(this._duration == 0){
            this._isOpening = false;
            this._isWating = true;
            this._duration = this._waitTime;
        }
        this.refresh();
    }else if(this._isWating){
        this._duration--;
        if (this._duration == 0) {
            this._isWating = false;
            this._isClosing = true;
            this._duration = 30;
        }
    }else if(this._isClosing){
        const x1 = 16;
        const x2 = -this.width;
        this.x = easeIn(x1, x2, d, 30);
        this.contents.paintOpacity = easeIn(255, 0, d, 30);
        this._duration--;
        if (this._duration == 0) {
            this._isClosing = false;
            this.hide();
        }
        this.refresh();
    }
};

// 言語設定テスト
Scene_Title.prototype.setLanguage = function() {
    const language = (window.navigator.languages && window.navigator.languages[0]) ||
                window.navigator.language ||
                window.navigator.userLanguage ||
                window.navigator.browserLanguage;

    if (language === 'ja') {
        $gameVariables.setValue(25, 0);
    }
};