/*
 * @Author: YeeChan
 * @Date: 2021-06-03 15:29:26
 * @Description: 
 */

'use strict';
let fs = require('fs');
let path = require('fire-path');

const images = Editor.require("packages://icon-generate/node_modules/images");

const iconList = [
  // 安卓
  { url: path.join('android', 'mipmap-ldpi', 'ic_launcher.png'), width: 36, height: 36 },
  { url: path.join('android', 'mipmap-mdpi', 'ic_launcher.png'), width: 48, height: 48 },
  { url: path.join('android', 'mipmap-hdpi', 'ic_launcher.png'), width: 72, height: 72 },
  { url: path.join('android', 'mipmap-xhdpi', 'ic_launcher.png'), width: 96, height: 96 },
  { url: path.join('android', 'mipmap-xxhdpi', 'ic_launcher.png'), width: 144, height: 144 },
  { url: path.join('android', 'mipmap-xxxhdpi', 'ic_launcher.png'), width: 192, height: 192 },
  { url: path.join('android', 'playstore-icon.png'), width: 512, height: 512 },
  // IOS
  { url: path.join('ios', 'AppIcon.appiconset', "icon-20-ipad.png"), width: 20, height: 20 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-20@2x-ipad.png"), width: 40, height: 40 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-20@2x.png"), width: 40, height: 40 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-20@3x.png"), width: 60, height: 60 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-29-ipad.png"), width: 29, height: 29 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-29.png"), width: 29, height: 29 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-29@2x-ipad.png"), width: 58, height: 58 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-29@2x.png"), width: 58, height: 58 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-29@3x.png"), width: 87, height: 87 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-40.png"), width: 40, height: 40 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-40@2x.png"), width: 80, height: 80 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-40@3x.png"), width: 120, height: 120 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-60@2x.png"), width: 120, height: 120 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-60@3x.png"), width: 180, height: 180 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-76.png"), width: 76, height: 76 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-76@2x.png"), width: 152, height: 152 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-83.5@2x.png"), width: 167, height: 167 },
  { url: path.join('ios', 'AppIcon.appiconset', "icon-1024.png"), width: 1024, height: 1024 }
];

Editor.Panel.extend({
  style: fs.readFileSync(Editor.url("packages://icon-generate/panel/index.css"), "utf-8"),
  template: fs.readFileSync(Editor.url("packages://icon-generate/panel/index.html"), "utf-8"),
  
  ready () {
    const app = new window.Vue({
      el: this.shadowRoot,
      created() {
        this.localIconPath = path.resolve(Editor.Project.path, "icon/");
        this.contentsPath = Editor.url("packages://icon-generate/Contents.json");
        
      },
      init() {
      },
      data: {
        contentsPath:'',
        localIconPath: '',
        uploadProgress: 0,

      },
      methods: {
        mkdir(dirName, mode) {
          if (fs.existsSync(dirName)) {
              return true;
          }
          if (this.mkdir(path.dirname(dirName), mode)) {
              fs.mkdirSync(dirName, mode);
              return true;
          }
        },

        mgm(src, dst, option) {
          let dstDir = path.parse(dst).dir;
          this.mkdir(dstDir);
          images(src)
              .size(option.width)
              .save(dst);
        },
        
        onBtnClickSave(event) {
          let iconPath = path.join(this.localIconPath,"icon.png")
          if (!fs.existsSync(iconPath)) {
            Editor.log("没有找到icon/icon.png");
            return;
          }
          // //android 文件夹
          // let androidPath = path.join(this.localIconPath,"android/")
          // if(fs.existsSync(androidPath)){
          //   fs.rmdirSync(androidPath);
          // }
          // fs.mkdirSync(androidPath);
          
          // //ios 文件夹
          // let iosPath = path.join(this.localIconPath,"ios/")
          // if(fs.existsSync(iosPath)){
          //   fs.rmdirSync(iosPath);
          // }
          // fs.mkdirSync(iosPath);
          

          this.uploadProgress = 0
          for (let i = 0; i < iconList.length; i++) {
            let icon = iconList[i];
            let progess = Math.round(i / iconList.length * 100)
            this.mgm(iconPath, path.join(this.localIconPath, icon.url), { width: icon.width, height: icon.height });
            
            this.uploadProgress = progess;
          }
          this.uploadProgress = 100
          // IOS 描述文件
          fs.copyFileSync(this.contentsPath, path.join(this.localIconPath, 'ios', 'AppIcon.appiconset', 'Contents.json'));

          Editor.log("全部已完成,生成路径:"+this.localIconPath);
        }
        
      },
    })
  },
});