import {
  getPackageInfo,
} from 'utils/file'

import path from 'path'
import OssInstance from 'utils/oss'
import axios from 'axios'

const fsEx = require('fs-extra')


export  async function uploadOssCallback() {
  return new Promise((resolve) => {
    axios
      .get('https://ark-font.oss-cn-hangzhou.aliyuncs.com/oss.json') // 获取cdn列表
      .then(async (res) => {
        const result = res.data
        if (result.code !== 200) {
          return
        }
        const oldJson = JSON.parse(JSON.stringify(result.data))
        const packageInfo = getPackageInfo()
        const packageName = packageInfo.name
        const packageVersion = packageInfo.version

        if (!packageName || !packageVersion) {
          return
        }
        if (
          ['@syman/ark-ui', '@syman/ark-ui-bcl', '@syman/burgeon-r3'].includes(
            packageName
          )
        ) {
          const index = result.data[packageName].findIndex((item: string) =>
            item.includes(packageVersion)
          )

          // 如果版本列表里没有这次发版信息，则添加。并创建上一次json的备份文件(oss.backup.json)
          if (index > -1) {
            // console.log('已存在', result.data[packageName][index])
            return
          }
          if (packageName === '@syman/ark-ui') {
            result.data[packageName].push(
              `https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/ark.min.js`,
              `https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/styles/ark-ui.css`,
              `https://oss.font.burgeononline.com/CDN/@syman/ark-ui/${packageVersion}/styles/bjIconfonts/iconfont.css`
            )
          }
          if (packageName === '@syman/ark-ui-bcl') {
            result.data[packageName].push(
              `https://oss.font.burgeononline.com/CDN/@syman/ark-ui-bcl/${packageVersion}/ark.bcl.min.css`,
              `https://oss.font.burgeononline.com/CDN/@syman/ark-ui-bcl/${packageVersion}/ark.bcl.min.js`
            )
          }
          if (packageName === '@syman/burgeon-r3') {
            result.data[packageName].push(
              `https://oss.font.burgeononline.com/CDN/@syman/burgeon-r3/${packageVersion}/r3.min.css`,
              `https://oss.font.burgeononline.com/CDN/@syman/burgeon-r3/${packageVersion}/r3.min.js`
            )
          }
        }

        const ossPath = path.resolve(__dirname, '../../temp/oss.json')
        const oldOssPath = path.resolve(__dirname, '../../temp/oss.backup.json')
        fsEx.ensureFileSync(path.resolve(__dirname, ossPath))
        fsEx.ensureFileSync(path.resolve(__dirname, oldOssPath))
        fsEx.writeJsonSync(ossPath, result, { spaces: 2 })
        fsEx.writeJsonSync(oldOssPath, oldJson, { spaces: 2 })
        const isSuccess = await OssInstance.uploadFiles(
          '/oss.json',
          path.resolve(__dirname, ossPath)
        )
        if (isSuccess) {
          await OssInstance.uploadFiles(
            '/oss.backup.json',
            path.resolve(__dirname, oldOssPath)
          )
        }
      })
      .finally(() => {
        resolve(true)
      })
  })
}


export  async function uploadOssDocCallback() {
  // 文档上传
  return new Promise((resolve) => {
    axios
      .get('https://ark-font.oss-cn-hangzhou.aliyuncs.com/ossDoc.json') // 获取cdn列表
      .then(async (res) => {
        const result = res.data
        if (result.code !== 200) {
          return
        }
        const oldJson = JSON.parse(JSON.stringify(result.data))
        const packageInfo = getPackageInfo()
        const packageName = packageInfo.name
        result.data[packageName] = `https://oss.font.burgeononline.com/DOC/${packageName}/index.html`;
        const ossPath = path.resolve(__dirname, '../../temp/ossDoc.json')
        const oldOssPath = path.resolve(__dirname, '../../temp/ossDoc.backup.json')
        fsEx.ensureFileSync(path.resolve(__dirname, ossPath))
        fsEx.ensureFileSync(path.resolve(__dirname, oldOssPath))
        fsEx.writeJsonSync(ossPath, result, { spaces: 2 })
        fsEx.writeJsonSync(oldOssPath, oldJson, { spaces: 2 })
        const isSuccess = await OssInstance.uploadFiles(
          '/ossDoc.json',
          path.resolve(__dirname, ossPath)
        )
        if (isSuccess) {
          await OssInstance.uploadFiles(
            '/ossDoc.backup.json',
            path.resolve(__dirname, oldOssPath)
          )
        }
      })
      .finally(() => {
        resolve(true)
      })
  })
}

