<template>
  <div class="market">
    <a-carousel v-if="bannerList && !!bannerList.length" arrows>
      <div
        slot="prevArrow"
        slot-scope="props"
        class="custom-slick-arrow"
        style="left: 10px; zindex: 1"
      >
        <a-icon type="left-circle" />
      </div>
      <div
        slot="nextArrow"
        slot-scope="props"
        class="custom-slick-arrow"
        style="right: 10px"
      >
        <a-icon type="right-circle" />
      </div>
      <div v-for="banner in bannerList">
        <img @click="jumpTo(banner.link)" width="100%" :src="banner.src" />
      </div>
    </a-carousel>
    <a-divider v-if="bannerList && !!bannerList.length"></a-divider>
    <h2>插件</h2>
    <a-list
      item-layout="horizontal"
      style="width: 100%"
      :grid="{ gutter: 16, column: 2 }"
      :data-source="pluginList"
    >
      <a-list-item slot="renderItem" slot-scope="item, index">
        <a-button
          v-if="showButton(item)"
          :loading="loading[index]"
          type="link"
          slot="actions"
          @click="download(index, item)"
        >
          <a-icon
            v-show="!loading[index]"
            style="font-size: 20px"
            type="cloud-download"
          />
        </a-button>

        <a-list-item-meta
          @click="showPannel(item, index)"
          :description="item.description"
        >
          <div slot="title">{{ item.pluginName }}</div>
          <a-avatar slot="avatar" :src="item.logo" />
        </a-list-item-meta>
      </a-list-item>
    </a-list>
    <a-drawer
      placement="right"
      :visible="show"
      @close="show = false"
      width="100%"
    >
      <div class="plugin-market-desc" slot="title">
        <img width="80" :src="currentSelect.logo" />
        <div class="desc">
          <h4>{{ currentSelect.pluginName }}</h4>
          <div class="info">
            <div class="actor">
              开发者：{{ currentSelect.author }}
              <a-button
                v-if="showButton(currentSelect)"
                :loading="loading[currentSelect.index]"
                @click="download(currentSelect.index, currentSelect)"
                icon="cloud-download"
                type="primary"
              >
                获取
              </a-button>
            </div>
            <div>{{ currentSelect.description }}</div>
          </div>
        </div>
      </div>
      <div class="market-plugin-detail" v-html="readme"></div>
    </a-drawer>
  </div>
</template>

<script>
import api from "../../../assets/api";
import { mapActions, mapState } from "vuex";
import marked from "marked";
import { shell } from "electron";
import path from "path";
const rendererMD = new marked.Renderer();

export default {
  data() {
    return {
      pluginList: [],
      loading: {},
      bannerList: [],
      show: false,
      currentSelect: {}
    };
  },
  async created() {
    // const [result, bannerRes] = await Promise.all([
    //   api.plugin.query(),
    //   api.banner.query(),
    // ]);
    // this.pluginList = result.result;
    // this.pluginList = [
    //   {
    //     pluginName: "日报",
    //     name: "日报",
    //     author: "璃白",
    //     detail: "# md",
    //     description: "gitcode团队日报",
    //     logo: "https://infinityicon.infinitynewtab.com/user-share-icon/6e49210c084629259f22609980c48ecf.png?imageMogr2/thumbnail/240x/format/webp/blur/1x0/quality/100|imageslim",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/report.zip"
    //   },
    //   {
    //     pluginName: "微信",
    //     name: "微信",
    //     author: "璃白",
    //     detail: "# weixin",
    //     description: "微信网页版",
    //     logo: "https://infinityicon.infinitynewtab.com/user-share-icon/2fc65bd66987ba1596324d55c01bddd7.png?imageMogr2/thumbnail/240x/format/webp/blur/1x0/quality/100|imageslim",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/weixin.zip"
    //   },
    //   {
    //     pluginName: "博客",
    //     name: "博客",
    //     author: "璃白",
    //     detail: "# blog",
    //     description: "CSDN博客离线版",
    //     logo: "https://gitcode.net/codechina/operation-work/uploads/48886bea190eb24ef4cf0499e589554c/1_cpongo9_1634797583.gif",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/blog.zip"
    //   },
    //   {
    //     pluginName: "提问",
    //     name: "提问",
    //     author: "璃白",
    //     detail: "# ask",
    //     description: "CSDN问答",
    //     logo: "https://gimg2.baidu.com/image_search/src=http%3A%2F%2Fpic.51yuansu.com%2Fpic2%2Fcover%2F00%2F40%2F87%2F581336a807c58_610.jpg&refer=http%3A%2F%2Fpic.51yuansu.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1642296911&t=4197c2560e18ac6b625d5d4b4cc3ece8",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/ask.zip"
    //   },
    //   {
    //     pluginName: "计算器",
    //     name: "计算器",
    //     author: "璃白",
    //     detail: "# 计算器",
    //     description: "计算器网页版",
    //     logo: "http://mms1.baidu.com/it/u=1258866987,457977032&fm=253&app=138&f=JPEG&fmt=auto&q=75?w=500&h=500",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/calc.zip"
    //   },
    //   {
    //     pluginName: "md编辑器",
    //     name: "md编辑器",
    //     author: "璃白",
    //     detail: "# md",
    //     description:
    //       "由 gitcode 技术团队开发的一款Markdown编辑器，支持使用markodwn语法来编写文档，同时支持图片上传等功能。",
    //     logo: "https://gitcode.net/uploads/-/system/project/avatar/43664/2111050605131483.png",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/md.zip"
    //   },
    //   {
    //     pluginName: "qrcode",
    //     name: "qrcode",
    //     description: "二维码识别",
    //     logo: "https://gitcode.net/codechina/operation-work/uploads/03cd9d3883254d315ba0aeca5643c65c/logo.png",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/qrcode.zip"
    //   },
    //   {
    //     pluginName: "toolsfun",
    //     name: "toolsfun",
    //     description: "toolfun工具箱",
    //     logo: "http://mms1.baidu.com/it/u=4045568405,1871612257&fm=253&app=138&f=JPG&fmt=auto&q=75?w=500&h=500",
    //     downloadUrl:
    //       "https://quicker-1253693946.cos.ap-beijing.myqcloud.com/plugins/toolsfun.zip"
    //   }
    // ];

    this.getPlugins();
    // this.bannerList = bannerRes.result;
  },

  methods: {
    getPlugins() {
      api.getPlugins().then(({ data: res }) => {
        this.pluginList = res;
      });
    },
    async download(index, item) {
      if (this.loading[index]) return;
      this.$set(this.loading, index, true);
      try {
        await this.downloadPlugin(item);
      } catch (e) {
        this.$message.error(e);
      }
      this.$set(this.loading, index, false);
    },
    showButton(item) {
      return !this.devPlugins.filter(
        plugin => plugin.name === item.name && plugin.type === "prod"
      ).length;
    },
    showPannel(item, index) {
      this.show = true;
      this.currentSelect = item;
      this.currentSelect.index = index;
    },
    jumpTo(link) {
      if (link) {
        shell.openExternal(link);
      }
    },
    ...mapActions("main", ["downloadPlugin"])
  },
  computed: {
    ...mapState("main", ["devPlugins"]),
    readme() {
      marked.setOptions({
        renderer: rendererMD,
        gfm: true,
        tables: true,
        breaks: false,
        pedantic: false,
        sanitize: false,
        smartLists: true,
        smartypants: false
      });
      try {
        return marked(this.currentSelect.detail);
      } catch (e) {
        return "暂无描述信息";
      }
    }
  }
};
</script>

<style lang="less">
.market-plugin-detail {
  img {
    width: 100%;
  }
  height: 430px;
  overflow: auto;
  &::-webkit-scrollbar {
    width: 0;
  }
}
.plugin-market-desc {
  display: flex;
  img {
    margin-right: 20px;
  }
  .desc {
    flex: 1;
  }
  .info {
    font-size: 12px;
    color: #999;
    font-weight: normal;
    padding-right: 40px;
  }
  .actor {
    flex: 1;
    display: flex;
    justify-content: space-between;
  }
}
.market {
  height: calc(~"100vh - 110px");
  background: #fff;
  padding: 20px;
  box-sizing: border-box;
  overflow: auto;
  .ant-carousel .slick-slide {
    text-align: center;
    height: 235px;
    line-height: 160px;
    background: #364d79;
    overflow: hidden;
  }

  .ant-carousel .custom-slick-arrow {
    width: 25px;
    height: 25px;
    font-size: 25px;
    color: #fff;
    background-color: rgba(31, 45, 61, 0.11);
    opacity: 0.3;
  }
  .ant-carousel .custom-slick-arrow:before {
    display: none;
  }
  .ant-carousel .custom-slick-arrow:hover {
    opacity: 0.5;
  }

  .ant-carousel .slick-slide h3 {
    color: #fff;
  }
  .ant-list-item {
    display: flex !important;
    align-items: center;
    justify-content: space-between;
  }
  .ant-list-item-meta {
    align-items: center;
  }
  .ant-list-item-meta-description {
    width: 200px;
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
}
</style>
