var tabData = [{"id":"1083712862729134080","name":"打印机","sort":1,"childList":[{"id":13,"printerType":"Bambu Lab P2S","img":"https://cms.bblmw.cn/p2s_9c0d9f2ea2.png","sort":0},{"id":15,"printerType":"Bambu Lab H2C","img":"https://cms.bblmw.cn/Frame_112386648122222222_fa736665af.png","sort":1},{"id":10,"printerType":"Bambu Lab H2D","img":"https://cms.bblmw.cn/H2_D_707d0aad42.jpg","sort":2},{"id":12,"printerType":"Bambu Lab H2S","img":"https://cms.bblmw.cn/H2_S_3a0c3a1ca4.png","sort":3},{"id":8,"printerType":"Bambu Lab A1","img":"https://cms.bblmw.cn/Group_47969_3x_bec3e4fd04.jpg","sort":4},{"id":3,"printerType":"Bambu Lab A1 mini","img":"https://cms.bblmw.cn/large_Bambu_Lab_A1_mini_8a462ea54c_21e037b9d7_9bb73390d0.png","sort":5},{"id":6,"printerType":"Bambu Lab P1S","img":"https://cms.bblmw.cn/COVER_b084d22761.png","sort":6},{"id":9,"printerType":"Bambu Lab X1C","img":"https://cms.bblmw.cn/X1_C_f429ebb303.JPEG","sort":7}]},{"id":"1083712862729134081","name":"软件","sort":2,"childList":[{"id":5,"printerType":"Bambu Studio","img":"https://cms.bblmw.cn/20250530_190020_7b93deceed.png","sort":0},{"id":7,"printerType":"Bambu Handy","img":"https://cms.bblmw.cn/zh_5d9e7c4dbe.png","sort":1},{"id":11,"printerType":"Bambu Suite","img":"https://cms.bblmw.cn/suite_zh_b1b3e231f8.png","sort":2}]},{"id":"1083712862729134082","name":"创意工坊","sort":3,"childList":[{"id":18,"printerType":"彩色版画生成器","img":"https://cms.bblmw.cn/2_6f482b511d.png","sort":0},{"id":19,"printerType":"2026 新春模型生成器","img":"https://cms.bblmw.cn/2_ad2f55ef51.png","sort":1},{"id":20,"printerType":"印你手办生成器","img":"https://cms.bblmw.cn/printu_academy_cover1_c3c74c8a0f.png","sort":2}]},{"id":"1083712862729134083","name":"耗材","sort":4,"childList":[{"id":16,"printerType":"拓竹耗材","img":"https://cms.bblmw.cn/20251223_150528222222222_1dfb0c4dab.png","sort":0}]}];

var youtubeData = [
  {
    "id": "8TQCRVS72Us",
    "title": "Intro: Overview of Bambu Studio"
  },
  {
    "id": "AdHUVQiVeDI",
    "title": "Episode 1: Set Up Your Environment"
  },
  {
    "id": "LDrCjCPYbhE",
    "title": "Episode 2: Import 3D Printing Models"
  },
  {
    "id": "3MYKsbd7k0c",
    "title": "Episode 3: Adjust the Models to Be Printable - Part 1"
  },
  {
    "id": "KcMBajgOzS0",
    "title": "Episode 4: Adjust the Models to Be Printable - Part 2"
  },
  {
    "id": "4W4vczKsiX8",
    "title": "Episode 5: Setup for Filaments"
  },
  {
    "id": "lQ4ySwPQeY4",
    "title": "Episode 6: Color Models for Multi-Color Prints"
  },
  {
    "id": "3tft_Ahkd_w",
    "title": "Episode 7: Divide and Print Your Models on Multiple Plates"
  },
  {
    "id": "ZH9xqdQYXsI",
    "title": "Episode 8: The Basic Quality Settings"
  },
  {
    "id": "FRLympzHsWY",
    "title": "Episode 9: The Basic Strength Settings"
  },
  {
    "id": "1QJ2UKDamzc",
    "title": "Episode 10: The Basic Support Settings"
  },
  {
    "id": "EHZtnnQLtGs",
    "title": "Episode 11: The Basic Other Settings"
  },
  {
    "id": "DWna4Mwjs5c",
    "title": "Episode 12: Check Your Sliced Model in Preview"
  },
  {
    "id": "pnd1CZ-Dp34",
    "title": "Episode 13: Send a Print Job to Your Printer"
  },
  {
    "id": "SZCWxYeMZro",
    "title": "Episode 14: During the Print"
  },
];

var topicData = [
  {
    "title": "Quick Start",
    "zhcn-title": "快速开始",
    "children": [
      {
        "title": "Bambu Studio Quick Start Guide",
        "zhcn-title": "Bambu Studio 快速上手教程",
        "link": "software/bambu-studio/studio-quick-start"
      },
      {
        "title": "Remote Control & Monitoring",
        "zhcn-title": "远程控制和监控打印机",
        "link": "software/bambu-studio/remote-control"
      },
      {
        "title": "Use AMS on Bambu Studio",
        "zhcn-title": "在 Bambu Studio 中使用 AMS",
        "link": "software/bambu-studio/use-ams-on-bambu-studio"
      },
      {
        "title": "Bambu Studio Multi Plate Printing Guide",
        "zhcn-title": "Bambu Studio 多盘打印指南",
        "link": "studio-handy/multi-plate-printing"
      }, 
    ]
  },
  {
    "title": "Gizmo",
    "zhcn-title": "顶部工具栏",
    "children": [
      {
        "title": "Variable Layer Height",
        "zhcn-title": "可变层高",
        "link": "software/bambu-studio/adaptive-layer-height"
      },
      {
        "title": "Cut Tool",
        "zhcn-title": "切割工具",
        "link": "software/bambu-studio/cut-tool"
      },
      {
        "title": "Auto Orientation",
        "zhcn-title": "自动朝向",
        "link": "software/bambu-studio/auto-orientation"
      },
      {
        "title": "Split to Objects/Parts",
        "zhcn-title": "拆分为对象/零件",
        "link": "software/bambu-studio/split-to-objects-parts"
      },
      {
        "title": "Support Painting Guide",
        "zhcn-title": "支撑绘制",
        "link": "software/bambu-studio/support-painting"
      },
      {
        "title": "3D Text",
        "zhcn-title": "添加 3D 文本",
        "link": "software/bambu-studio/3d-text"
      }
    ]
  },
  {
    "title": "Right-click Tool",
    "zhcn-title": "右键工具",
    "children": [
      {
        "title": "Fix Model",
        "zhcn-title": "修复模型",
        "link": "software/bambu-studio/fix-model"
      },
      {
        "title": "Simplify Model",
        "zhcn-title": "简化模型",
        "link": "software/bambu-studio/simplify-model"
      },
      {
        "title": "Negative Part",
        "zhcn-title": "负零件",
        "link": "software/bambu-studio/subtract-a-part"
      },
      {
        "title": "Bambu Studio Modifier Operation Guide",
        "zhcn-title": "Studio 修改器操作指南",
        "link": "software/bambu-studio/modifier"
      }
    ]
  },
  {
    "title": "Multi-material Printing",
    "zhcn-title": "多材料打印",
    "children": [
      {
        "title": "Introduction to Filament Grouping Strategy for Dual Nozzle Printers",   
        "zhcn-title": "双喷嘴打印机耗材切片分组",
        "link": "software/bambu-studio/manual/dual-nozzles-slicing-filament-grouping"
      },
      {
        "title": "Multi-Color Printing",   
        "zhcn-title": "多色打印指南",
        "link": "software/bambu-studio/multi-color-printing"
      },
      {
        "title": "Color Painting Tool",   
        "zhcn-title": "涂色工具使用指南",
        "link": "software/bambu-studio/color-painting-tool"
      },
      {
        "title": "Reduce Waste during Filament Change",   
        "zhcn-title": "减少多色打印时的材料浪费",
        "link": "software/bambu-studio/reduce-wasting-during-filament-change"
      },
    ]
  },
  {
    "title": "Print Settings",
    "zhcn-title": "打印设置",
    "children": [
      {
        "title": "Special Slicing Mode in Bambu Studio",
        "zhcn-title": "Bambu Studio 特殊切片模式",
        "link": "software/bambu-studio/special-slicing-modes"
      },
      {
        "title": "How to Create Custom Preset",
        "zhcn-title": "创建自定义预设",
        "link": "software/bambu-studio/preset"
      },
      {
        "title": "Seam Settings",
        "zhcn-title": "接缝设置",
        "link": "software/bambu-studio/Seam"
      },
      {
        "title": "Support settings in Bambu Studio",
        "zhcn-title": "支撑耗材与支撑功能的介绍",
        "link": "software/bambu-studio/support"
      },
      {
        "title": "Brim",
        "zhcn-title": "Brim",
        "link": "software/bambu-studio/auto-brim"
      },
    ]
  },
  {
    "title": "Calibration",
    "zhcn-title": "流量校准",
    "children": [
      {
        "title": "Flow Rate Calibration",
        "zhcn-title": "流量比例",
        "link": "software/bambu-studio/calibration_flow_rate"
      },
      {
        "title": "Flow Dynamics Calibration",
        "zhcn-title": "动态流量校准",
        "link": "software/bambu-studio/calibration_pa"
      }
    ]
  },
  {
    "title": "Troubleshooting",
    "zhcn-title": "故障排除",
    "children": [
      {
        "title": "Failed to send print files",
        "zhcn-title": "无法发送打印文件",
        "link": "software/bambu-studio/failed-to-send-print-files"
      },
      {
        "title": "Fails of live-view",
        "zhcn-title": "加载视频流失败排查",
        "link": "software/bambu-studio/faq/live-view"
      },
      {
        "title": "Bambu Studio crashes/freezes troubleshooting guide",
        "zhcn-title": "Bambu Studio 崩溃/冻结故障排除指南",
        "link": "bambu-studio/troubleshoot/crash-freeze-issue"
      },
      {
        "title": "Export Bambu Studio software log",
        "zhcn-title": "导出Bambu PC软件日志",
        "link": "x1/troubleshooting/bambu-studio-log"
      },
      {
        "title": "Bambu Studio failed to Connect Printer",
        "zhcn-title": "Bambu Studio 连接打印机失败",
        "link": "software/bambu-studio/failed-to-connect-printer"
      },
    ]
  }
];

var $prev;
var $next;

var $video_prev;
var $video_next;

function OnInit() {
  $prev = $('#academy_Left_Btn');
  $next = $('#academy_Right_Btn');

  getAcademyData();
  createVideoHTML();
  if (IsChinese())
    $("#tutorial_block").hide();
  createTopicHTML();
  TranslatePage();

  $prev.on('click', () => scrollByStep(-1));
  $next.on('click', () => scrollByStep(+1));
  $('#academy_content').on('scroll', updateButtons);
  $(window).on('resize', updateButtons);
  updateButtons();

  $video_prev = $('#tutorial_Left_Btn');
  $video_next = $('#tutorial_Right_Btn');
  $video_prev.on('click', () => scrollVideoByStep(-1));
  $video_next.on('click', () => scrollVideoByStep(+1));
  $('#tutorial_content').on('scroll', updateVideoButtons);
  $(window).on('resize', updateVideoButtons);
  updateVideoButtons();

  $("#Wiki_Search_Input").on("input", debounce(function() {
    let keyword = $(this).val();
    searchWiki(keyword);
  }, 500));
  $("#Wiki_Search_Input").on("focus", function () {
    if (this.value.trim().length) {
      $(this).trigger("input");
    }
  });

  // $(".searchResult").on("click", function(e) {
  //   console.log(e);
  // });
  $(document).on('click', function (e) {
    if (!$(e.target).closest('.search').length) {
      $('#search_result_area').empty();
    }
  });
}

function debounce(fn, delay){
  let t;
  return function(...args){
    clearTimeout(t);
    t = setTimeout(() => fn.apply(this, args), delay);
  };
}

// search

function searchWiki(keyword) {
  var tSend={};
	tSend['sequence_id']=Math.round(new Date() / 1000);
	tSend['command']="search_wiki";
  tSend['data']={};
  tSend['data']['keyword']=keyword;

	SendWXMessage( JSON.stringify(tSend) );
}

function updateSearchResult(result) {
  let data = result["pages"]["search"];
  $('#search_result_area').empty();
  if (data["totalHits"] > 0) {
    data["results"].forEach(element => {
      if (isMainland() || IsChinese()){
        if (element["locale"] != "zh")
          return;
      }else {
        if (element["locale"] != "en")
          return;
      }
      let html = `<div class="searchResult" onclick="openWikiUrl('${element.path}')">${element.title}</div>`;
      $('#search_result_area').append(html);
    });
  }
}

//--------------- Academy Tabs & Cards -------------------

var currentTabIdx = 0;
var currentTabData = [];

function initAcademyTabs(data) {
  currentTabData = data.slice().sort((a, b) => a.sort - b.sort);
  currentTabIdx = 0;
  $('#academy_tabs').empty();
  $('#academy_Card_Content').empty();
  $('#academy_content').scrollLeft(0);
  for (let i = 0; i < currentTabData.length; i++) {
    let activeClass = i === 0 ? 'active' : '';
    let html = `<div class="academyTab ${activeClass}" data-idx="${i}" onclick="switchAcademyTab(${i})">${currentTabData[i].name}</div>`;
    $('#academy_tabs').append(html);
  }
  renderAcademyCards(0);
}

function switchAcademyTab(idx) {
  if (idx === currentTabIdx) return;
  currentTabIdx = idx;
  $('.academyTab').removeClass('active');
  $(`.academyTab[data-idx="${idx}"]`).addClass('active');
  $('#academy_Card_Content').empty();
  $('#academy_content').scrollLeft(0);
  renderAcademyCards(idx);
}

function renderAcademyCards(idx) {
  let children = currentTabData[idx].childList.slice().sort((a, b) => a.sort - b.sort);
  for (let i = 0; i < children.length; i++) {
    let item = children[i];
    let html = `<div class="card" data-idx="${i}" onclick="openAcademyUrl('${item.id}')">
                  <img class="cardImg" src="${item.img}" />
                  <div class="cardTitle TextS1">${item.printerType}</div>
                </div>`;
    $('#academy_Card_Content').append(html);
  }
  updateButtons();
}

function getAcademyData() {
  var tSend={};
	tSend['sequence_id']=Math.round(new Date() / 1000);
	tSend['command']="get_academy_list";
	SendWXMessage( JSON.stringify(tSend) );
}

function stepCardSize() {
  const $first = $('#academy_Card_Content').children().first();
  if ($first.length === 0) return 0;
  let gap = 16;
  return $first.outerWidth(true) + gap;
}

function maxScrollLeft() {
  var a = $('#academy_Card_Content').outerWidth(true);
  var b = $('#academy_content').innerWidth();
  return Math.max(0, $('#academy_Card_Content').outerWidth(true) - $('#academy_content').innerWidth());
}

function clampScroll(x) {
  const max = maxScrollLeft();
  return Math.min(Math.max(0, x), max);
}

function updateButtons() {
  const x = Math.round($('#academy_content').scrollLeft());
  const max = Math.round(maxScrollLeft());
  if (max <= 0) {
    $prev.hide();
    $next.hide();
    return;
  }
  $prev.show();
  $next.show();
  $prev.prop('disabled', x <= 0);
  $next.prop('disabled', x >= max);
}

function scrollByStep(dir) {
  const step = stepCardSize();
  if (!step) return;
  const current = $('#academy_content').scrollLeft();
  const target = clampScroll(current + dir * step);
  $('#academy_content').stop(true, false).animate({ scrollLeft: target }, 260, 'swing', updateButtons);
}

function openAcademyUrl(id)
{
  let open_url = "";
  if (isMainland()){
    open_url = "https://bambulab.cn/zh-cn/support/academy/";
  }else{
    let strLang=langStringTransfer();
    open_url = "https://bambulab.com/" + strLang + "/support/academy/";
  }
  open_url += id;
  OpenUrlInLocalBrowser(open_url);
}

function openWebsiteUrl(id)
{
  let open_url = "";
  if (isMainland()){
    open_url = "https://bambulab.cn/zh-cn/";
  }else{
    let strLang=langStringTransfer();
    open_url = "https://bambulab.com/" + strLang + "/";
  }
  open_url += id;
  OpenUrlInLocalBrowser(open_url);
}


// ---------------- Tutorials -------------------

function createVideoHTML() {
  let academyData = youtubeData;
  for (let i = 0; i < academyData.length; i++) {
    let html = `<div class="videoCard" onclick="openVideoUrl('${academyData[i].id}')">
                  <img class="videoThumbnail" src="img/${academyData[i].id}.jpg" />
                  <div class="videoTitle TextS1">${academyData[i].title}</div>
                </div>`;
    $('#tutorial_Card_Content').append(html)
  }
}

function stepVideoCardSize() {
  const $first = $('#tutorial_Card_Content').children().first();
  if ($first.length === 0) return 0;
  let gap = 16;
  return $first.outerWidth(true) + gap;
}

function maxVideoScrollLeft() {
  var a = $('#tutorial_Card_Content').outerWidth(true);
  var b = $('#tutorial_content').innerWidth();
  return Math.max(0, $('#tutorial_Card_Content').outerWidth(true) - $('#tutorial_content').innerWidth());
}

function videoClampScroll(x) {
  const max = maxVideoScrollLeft();
  return Math.min(Math.max(0, x), max);
}

function updateVideoButtons() {
  const x = Math.round($('#tutorial_content').scrollLeft());
  const max = Math.round(maxVideoScrollLeft());
  $video_prev.prop('disabled', x <= 0);
  $video_next.prop('disabled', x >= max);
}

function scrollVideoByStep(dir) {
  const step = stepVideoCardSize();
  if (!step) return;
  const current = $('#tutorial_content').scrollLeft();
  const target = videoClampScroll(current + dir * step);
  $('#tutorial_content').stop(true, false).animate({ scrollLeft: target }, 260, 'swing', updateVideoButtons);
}

function openVideoUrl(path)
{
  let open_url = "https://www.youtube.com/watch?v=";
  open_url += path;
  OpenUrlInLocalBrowser(open_url);
}

// ---------------- topic -------------------

function createTopicHTML() {
  for (let i = 0; i < topicData.length; i++) {
    let title;
    if (IsChinese()){
      title = topicData[i]['zhcn-title'];
    } else {
      title = topicData[i].title;
    }
    let html = `<div class="topicCard">
          <div class="topicTitle TextS1">${title}</div>
          <ul>`
    for (let j=0; j < topicData[i].children.length; j++) {
      let child_title;
      if (IsChinese()){
        child_title = topicData[i].children[j]['zhcn-title'];
      } else {
        child_title = topicData[i].children[j].title;
      }
      html += `<li onclick="openWikiUrl('${topicData[i].children[j].link}')">${child_title}</li>`;
    }
    html += `</ul></div>`;
    $('#topic_content').append(html)
  }
}

function openWikiUrl(path)
{
  let open_url = "https://wiki.bambulab.com/"
  if (IsChinese()){
    open_url += "zh/";
  }else{
    open_url += "en/";
  }
  open_url += path;

  var tSend={};
	tSend['sequence_id']=Math.round(new Date() / 1000);
	tSend['command']="userguide_wiki_open";
	tSend['data']={};
	tSend['data']['url']=open_url;
	
	SendWXMessage( JSON.stringify(tSend) );	
}

// --------------------common ----------------

function IsChinese()
{
	let strLang=GetQueryString("lang");
	if(strLang!=null)
	{}else{
		strLang=localStorage.getItem(LANG_COOKIE_NAME);
	}
	
	if(strLang!=null)
		return strLang.includes('zh')
	else
		return false;
}

function isMainland(){
  let strRegion=GetQueryString("region");
  return strRegion=="CN";
}

function langStringTransfer()
{
  let strLang=GetQueryString("lang");
  if(strLang==null)
    strLang=localStorage.getItem(LANG_COOKIE_NAME);
  
  if(strLang.includes('zh')){
    return 'zh';
  }else if(strLang.includes('en')){
    return 'en';
  }else if(strLang.includes('fr')){
    return 'fr-fr';
  }else if(strLang.includes('de')){
    return 'de-de';
  }else if(strLang.includes('es')){
    return 'es-mx';
  }else if(strLang.includes('it')){
    return 'it-it';
  }else if(strLang.includes('ja')){
    return 'ja-jp';
  }else if(strLang.includes('ko')){
    return 'ko-kr';
  }else if(strLang.includes('pt')){ 
    return 'pt-br';
  }else if(strLang.includes('nl')){
    return 'nl-nl';
  }else{
    return 'en';
  }
}

function HandleStudio( pVal )
{
  let strCmd = pVal['command'];
  if(strCmd=='search_wiki_get')
	{
		updateSearchResult(pVal['data']);
	}else if(strCmd=='academy_list_get') {
    initAcademyTabs(pVal['data']);
  }
}

//---------------Global-----------------
window.postMessage = HandleStudio;
