let menuHSearchPos = new Pos2D(185, 80); 
let NMH_GS_ini = Game_System.prototype.initialize;
Game_System.prototype.initialize = function () { 
    NMH_GS_ini.call(this);
    this.SComeArr = []; 
    this.SBbsArr = []; 
};
class SearchComment {   
    constructor(textArr, name , bbsData) {
        this._TxtArr = textArr; 
        this._Name = name;    
        this._Vflg = false;    
        this._BbsData = bbsData;
    }
}
class BbsData {
    constructor(topic , text , picPath) {
        this.topic = topic;
        this.text = text;
        this.picPath = picPath;
    }
}
let AddCmnt = function (textArr, name) {
    $gameSystem.SComeArr.push(new SearchComment(textArr, name , null));
}
let AddBbs = function (textArr, name , bbsTopic , bbsText , bbsPicPath) {
    bbsPicPath = typeof bbsPicPath !== 'undefined' ? bbsPicPath : "";
    $gameSystem.SBbsArr.push(new SearchComment(textArr, name , new BbsData(bbsTopic , bbsText , bbsPicPath)));
}
let ResetBbs = function () {
    $gameSystem.SBbsArr = [];
    menuHSearchPicArr = [];
    menuHSearhDatas = [];
}
let setCommentArr = []; 
let menuHSearchFlame = 0; 
let Menu_H_SearchMode = 0; 
let menuHSearchWinCount = 0; 
let menuHSearchWinMaxCnt = 0; 
let menuHSearchRndYArr = [];
var bbsFlg = false;
Game_Interpreter.prototype.Menu_H_SearchView = function (fadeInFlg) {
    fadeInFlg = typeof fadeInFlg !== 'undefined' ? fadeInFlg : false;
    ViewSpriteArr_Reset(); 
    let setOpacity = 255;
    if (fadeInFlg) {
        menuHSearchFlame = 0; 
        if (!bbsFlg)
            menuHSearchWinMaxCnt = $gameSystem.SComeArr.length - 1;
        else
            menuHSearchWinMaxCnt = $gameSystem.SBbsArr.length - 1;
        if (menuHSearchWinMaxCnt >= 9) menuHSearchWinMaxCnt = 9;
        menuHSearchWinCount = 0; 
        setOpacity = 0;
        menuHSearchRndYArr = randomGetArr([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 10);
    }
    if (!bbsFlg) {
        this.SetSprite(PN_SBase, Menu_H_UIPath + "Search/SBase", menuHSearchPos._x, menuHSearchPos._y, setOpacity);
    }
    setCommentArr = [];
    if (!bbsFlg) {
        let startIndex = $gameSystem.SComeArr.length - 10;
        if (startIndex < 0) startIndex = 0;
        for (let i = startIndex; i <= $gameSystem.SComeArr.length - 1; i++) {
            setCommentArr.push($gameSystem.SComeArr[i]);
        }
    } else {
        let startIndex = $gameSystem.SBbsArr.length - 10;
        if (startIndex < 0) startIndex = 0;
        for (let i = startIndex; i <= $gameSystem.SBbsArr.length - 1; i++) {
            setCommentArr.push($gameSystem.SBbsArr[i]);
        }
    }
    for (let i = 0; i <= ViewSpriteArr.length - 1; i++) {
        this.MoveSprite(ViewSpriteArr[i], true, true, 10, 255, true, true);
    }
    this.SetSpriteC(PN_BbsReload, Menu_H_UIPath + "Search/PN_BbsReload", menuHSearchPos._x + 544, menuHSearchPos._y + 671, 0);
    this.MoveSprite(PN_BbsReload, true, true, 10, 255, true, true);
}
let menuHSearchSetRndX = 350;
let menuHSearchSetRndY = 376;
let menuHSearchPicArr = []; 
let menuHSearhDatas = [];
Game_Interpreter.prototype.Menu_H_SearchUpdate = function () {
    if (menuHSearchFlame == 1 && !bbsFlg) {
        this.SetSprite(PN_SBack, Menu_H_UIPath + "Search/SBack", menuHSearchPos._x, menuHSearchPos._y, 0);
        this.MoveSprite(PN_SBack, true, true, 60, 255, true, true);
        menuHSearchPicArr = [];
        menuHSearhDatas = [];
    }
    this.SpriteCngColor(PN_BbsReload);
    if (overPointerSpCk(PN_BbsReload)) {
        this.SpriteCngColor( PN_BbsReload , [100,100,100,0]);
        if (TouchInput.isTriggered()) {
            this.DelSpriteSpan(PN_SWinS , PN_BbsPicture);
            menuHSearhDatas = [];
            this.Menu_H_SearchView(true);
            ShuffleArr(setCommentArr); 
            bbsDetailViewFlg = false; 
        }
    }
    if (menuHSearchFlame > 1 && menuHSearchFlame % 5 == 0 && menuHSearchWinCount <= setCommentArr.length - 1) {
        let outLineStr = "\\ow[0]"
        D_Text_Cng_font = "Makinas"; 
        let setX = menuHSearchPos._x + 58 + (350 * (menuHSearchRndYArr[menuHSearchWinCount] / 9));
        let setY = menuHSearchPos._y + 148 + (Math.random() * 376);
        let setPicNo = PN_SWinS + (menuHSearchWinCount * 5); 
        let viewFlame = 10; 
        if (bbsFlg) {   
            setX -= 100;
            menuHSearchPicArr.push(setPicNo);
            menuHSearhDatas.push(setCommentArr[menuHSearchWinCount]._BbsData);
        }
        let _SPar = 1 - (0.5 * ((menuHSearchWinMaxCnt - menuHSearchWinCount) / menuHSearchWinMaxCnt));
        if (menuHSearchWinMaxCnt <= 0) _SPar = 1;
        this.SetSprite(setPicNo + 0, Menu_H_UIPath + "Search/SWin", setX, setY, 0, 100 * _SPar, 100 * _SPar);
        this.MoveSprite(setPicNo + 0, true, true, viewFlame, 255, true, true);
        for (let i = 0; i <= setCommentArr[menuHSearchWinCount]._TxtArr.length - 1; i++) {
            this.SpriteStr(setPicNo + 1 + i, outLineStr + setCommentArr[menuHSearchWinCount]._TxtArr[i], 20 * _SPar,
                setX + 20, setY + (22 + (i * 23)) * _SPar, 0, 100 * _SPar, 100 * _SPar);
            this.MoveSprite(setPicNo + 1 + i, true, true, viewFlame, 255, true, true);
        }
        this.SpriteStrR(setPicNo + 4, outLineStr + setCommentArr[menuHSearchWinCount]._Name, 20 * _SPar,
            setX + (330 * _SPar), setY + (88 * _SPar), 0, 100 * _SPar, 100 * _SPar);
        this.MoveSprite(setPicNo + 4, true, true, viewFlame, 255, true, true);
        menuHSearchWinCount++;
        D_Text_Cng_font = ""; 
    }
    menuHSearchFlame++;
}
function randomGetArr(array, num) {
    let a = array;
    let t = [];
    let r = [];
    let l = a.length;
    let n = num < l ? num : l;
    while (n-- > 0) {
        let i = Math.random() * l | 0;
        r[n] = t[i] || a[i];
        --l;
        t[i] = t[l] || a[l];
    }
    return r;
}
Game_Interpreter.prototype.BBS_Viewer_Start = function () {
    bbsFlg = true;
    this.SetSprite(PN_SBack, Menu_H_UIPath + "Search/BbsBaseBack", 0 , 0 , 0);
    this.MoveSprite(PN_SBack, true, true, 20, 255, true, true);
    this.SetSprite(PN_SBack + 1, Menu_H_UIPath + "Search/BbsBase", 36 , 54 , 0);
    this.MoveSprite(PN_SBack + 1, true, true, 20, 255, true, true);
    this.SetSprite(PN_SBack + 2, Menu_H_UIPath + "Search/BbsBack", 36 , 54 , 0);
    this.MoveSprite(PN_SBack + 2, true, true, 20, 255, true, true);
    this.Menu_H_SearchView(true);
}
var bbsDetailViewFlg = false;
var bbsSelectPicNo = -1;
Game_Interpreter.prototype.BBS_Viewer_Update= function () {
    this.Menu_H_SearchUpdate();
    if (bbsDetailViewFlg) {
        this.BBS_DetailData_Update();
        return true;
    }
    this.SpriteCngColorArr(menuHSearchPicArr);
    var ckPicNo = overPointerSpCkArr(menuHSearchPicArr);
    if (ckPicNo != -1) {
        this.SpriteCngColor(ckPicNo , [80,80,80,0]);
        if (TouchInput.isTriggered()) {
            for (let i = 0; i < menuHSearchPicArr.length; i++) {
                if (ckPicNo == menuHSearchPicArr[i]) {
                    var ckData = menuHSearhDatas[i];
                    bbsSelectPicNo = menuHSearchPicArr[i];
                    this.BBS_DetailData_View(ckData);
                    bbsDetailViewFlg = true;
                    return true;
                }
            }
        }
    }
    Menu禁止Flame = 5;
    if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
        bbsFlg = false;
        this.DelSpriteSpan(PN_SBack , PN_BbsPicture);
        return false;
    }
    return true; 
}
Game_Interpreter.prototype.BBS_DetailData_View = function (_viewData) {
    this.SetSprite(PN_BbsDtl, Menu_H_UIPath + "Search/BbsDetail", 147 , 160)
    let outLineStr = "\\ow[0]"
    this.SpriteStr(PN_BbsTopic, outLineStr + _viewData.topic, 22, 200 , 225);
    var addText = "";
    for (let i = 0; i <= _viewData.text.length - 1; i++)
        addText += _viewData.text[i] + "\n";
    this.SpriteStr(PN_BbsText, outLineStr + addText, 22, 200 , 225 + 57);
    if (_viewData.picPath != "")
        this.SetSprite(PN_BbsPicture, Menu_H_UIPath + "Search/BbsDetailPic/" + _viewData.picPath , 147 , 160);
}
Game_Interpreter.prototype.BBS_DetailData_Update = function () {
    if (Input.isTriggered('menu') || TouchInput.isCancelled()) {
        this.DelSpriteSpan(PN_BbsDtl , PN_BbsPicture);
        bbsDetailViewFlg = false;
        this.DelSpriteSpan(bbsSelectPicNo , bbsSelectPicNo + 4);
    }
}
