<template>
  <div class="download">
    <Header :descText="'Play 5 Games in any mode with your registered E-mail'" />
    <h5>
      Complete this task to win a share of the
      <span>180,000 $CEC</span> prize pool
    </h5>
    <div class="login-content">
      <li>
        <div class="left">
          <div>
            <span v-if="!token">1</span>
            <img v-else src="@/assets/icon_done.png" alt />
          </div>
        </div>
        <div class="right">
          <h4>Bitget Wallet</h4>
          <div class="connect" v-if="!token">
            <span @click="walletLink">Connect</span>
          </div>
          <div class="address" v-else>{{ address }}</div>
            <div class="email" v-if="token && email">
              Game Account:
              <span>{{ email }}</span>
            </div>
        </div>
      </li>
      <li>
        <div class="left">
          <div>
            <span v-if="!email">2</span>
            <img v-else src="@/assets/icon_done.png" alt />
          </div>
        </div>
        <div class="right">
          <h4>Download the game</h4>
          <div class="download-content">
            <div class="download-content-counter-fire">
              <div class="counter-fire-title">Counter Fire:</div>
              <p>Anime-style battle royale and MOBA games.</p>
              <div class="counter-fire-btn">
                <div>
                  <a href="https://apps.apple.com/app/counter-fire/id6444462915" target="_blank">
                    <img src="@/assets/btn_appStore.png" alt />
                  </a>
                </div>
                <div>
                  <a href="https://play.google.com/store/apps/details?id=com.cege.games.release" target="_blank">
                    <img src="@/assets/btn_googlePlay.png" alt />
                  </a>
                </div>
              </div>
            </div>
          </div>
        </div>
      </li>
      <li>
        <div class="left">
          <div>
            <img
              v-if="missions.current != missions.target"
              src="@/assets/icon_done.png"
              alt
            />
            <span v-else>3</span>
          </div>
        </div>
        <div class="right">
          <h4>
            Completion Progress
            <span>
                <span>{{ missions.current }}</span>/{{ missions.target }}
            </span>
          </h4>
        </div>
      </li>

      <!-- <div @click="walletLink">Wallet Connect</div>
      <div>{{ address }}</div>
      <div @click="logOut">Wallet logOut</div>-->
    </div>
    <div class="dialog" v-if="isDownloadDialog">
      <div class="dialog-content">
        <div class="dialog-tips">
          <p>Please complete game account</p>
          <p>registration first</p>
        </div>
        <div class="dialog-btn">
          <div @click="toHome">
            <!-- <router-link to="/">Go</router-link> -->
            Go
          </div>
          <div class="close" @click="isDownloadDialog = false">Close</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import Header from "@/components/header.vue";
import { BitgetWallet } from "@/wallet/BitgetWallet.js";
import { apiProgress, getEmail } from "@/utils/request";

export default {
  components: {
    Header
  },
  data() {
    return {
      token: "",
      address: "",
      isDownloadDialog: false,
      email: "",
      missions: [
        {
          mission_id: 0,
          current: 0,
          target: 0,
        }
      ]
    };
  },
  mounted() {
    this.token = localStorage.getItem("token");
    this.address = localStorage.getItem("address");
    if (this.token) {
      this.email = getEmail(this.token);
      if (!this.email) {
        this.isDownloadDialog = true;
      }
      this.getProgress();
    }
  },
  methods: {
    // 登录
    async walletLink() {
      var isBitgetWallet = navigator.userAgent.indexOf('BitKeep') > 0
      if(!isBitgetWallet) {
        window.location.href = `https://bkcode.vip?action=dapp&url=${location.origin}/register`
      } else {
        let bitgetWallet = new BitgetWallet();
        let res = await bitgetWallet.getAccessToken();
        this.$store.commit("wallet/updateToken", res.token);
        localStorage.setItem("token", res.token);
        localStorage.setItem("address", res.accounts[0]);
        this.token = res.token;
        this.address = res.accounts[0];
        this.token = res.token;
        this.email = getEmail(this.token);
        this.getProgress();
      }
    },

    // 获取任务进度
    async getProgress() {
      if (this.email) {
        let res = await apiProgress(this.email);
        this.missions = res.missions[0];
      }
    },

    async getAddress() {
      let bitgetWallet = new BitgetWallet();
      let res = await bitgetWallet.web3Provider();
      // console.log(res)
    },

    // 下载
    // downloadGame(platform) {
    //   if (platform == "Android") {
    //     window.location.href =
    //       "https://counterfire.games/release/counterfire.apk";
    //   } else if (platform == "Google Play") {
    //     window.open(
    //       "https://play.google.com/store/apps/details?id=com.cege.games.release"
    //     );
    //   } else {
    //     window.open("https://apps.apple.com/app/counter-fire/id6444462915");
    //   }
    // },

    toHome() {
      this.$router.push({
        name: "Create",
        params: {
          name: "download"
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
.download {
  h5 {
    width: 320px;
    margin: 0 auto;
    font-family: "Poppins";
    font-weight: 400;
    font-size: 16px;
    // text-align: center;
    color: #ffffff;
    margin-top: 5px;
    span {
      color: #ffef67;
    }
  }
  .login-content {
    width: 370px;
    margin: 0 auto;
    // margin-left: 58.6px;
    margin-top: 35px;
    li {
      display: flex;
      min-height: 120px;
      color: #ffffff;
      position: relative;
      .left {
        width: 38px;
        height: 38px;
        display: flex;
        align-items: center;
        justify-content: center;
        background: #3c73ff;
        border-radius: 50%;
        span {
          font-family: "Anton";
          font-weight: 400;
          font-size: 20px;
          color: #000000;
        }
      }
      .right {
        // width: ;
        margin-left: 18.6px;
        position: relative;
        h4 {
          display: flex;
          align-items: center;
          font-family: "Poppins";
          font-weight: 400;
          font-size: 20px;
          margin-bottom: 12px;
          color: #ffffff;
          span {
            display: inline-block;
            padding: 5px 25px;
            background: #211d26;
            margin-left: 10px;
            border-radius: 30px;
            b {
              color: #37ffff;
            }
          }
        }
        .connect {
          width: 218px;
          height: 61px;
          line-height: 61px;
          text-align: center;
          background: #4936d7;
          font-family: "Anton";
          font-weight: 400;
          font-size: 24px;
          border-radius: 30px;
        }
        .address {
          position: absolute;
          padding: 5px 8px;
          border-radius: 15px;
          background: #211d26;
          font-size: 13px;
          color: #37ffff;
          font-family: "Poppins";
          font-weight: 400;
        }
        .email {
          position: absolute;
          top: 80px;
          width: 330px;
          height: 34px;
          line-height: 34px;
          padding: 0px 4px;
          padding-left: 15px;
          box-sizing: border-box;
          border-radius: 15px;
          background: #211d26;
          font-size: 12px;
          color: #fff;
          font-family: "Poppins";
          font-weight: 400;
          span {
            font-size: 13px;
            font-family: "Poppins";
            font-weight: bold;
          }
        }
        .download-content {
          // margin-top: 10px;
          height: 160px;
          background: #37ffff;
          background: url("@/assets/gameRole.png") no-repeat;
          background-size: 100% 100%;
          width: 450px;
          position: absolute;
          left: -107px;
          .download-content-counter-fire {
            width: 353px;
            height: 137px;
            margin: 0 auto;
            margin-top: 105px;
            margin-left: 80px;
            background: #17141b;
            border-radius: 44px;
            border: 2px solid #b966ff;
            padding: 19px 14px;
            box-sizing: border-box;
            .counter-fire-title {
              font-size: 14px;
              font-family: "Poppins-SemiBold";
              font-weight: bold;
            }
            p {
              font-size: 14px;
              color: #9950fd;
            }
            .counter-fire-btn {
              display: flex;
              justify-content: space-between;
              margin-top: 17px;
              div {
                width: 153px;
                height: 50px;
                img {
                  width: 100%;
                  height: 100%;
                }
              }
            }
          }
        }
      }
      &:nth-child(2) {
        height: 295px;
      }
      &:nth-child(3) {
        min-height: 40px !important;
        .right {
          height: 70px;
          span {
            span {
              padding: 0;
              margin: 0;
              color: #37ffff;
            }
          }
        }
      }
      &::after {
        content: "";
        position: absolute;
        top: 30px;
        left: 18px;
        width: 1px;
        height: calc(100% - 30px);
        background: #3ceefe;
        z-index: 1;
      }
      &:last-child {
        &::after {
          display: none;
        }
      }
    }
  }
  .dialog {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: rgba(0, 0, 0, 0.5);
    z-index: 9;
    .dialog-content {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%) !important;
      width: 400px;
      height: 298px;
      background: #17141b;
      border-radius: 66px;
      border: 2px solid #b966ff;
      .dialog-tips {
        color: #fff;
        font-size: 21px;
        font-family: "Poppins-SemiBold";
        font-weight: bold;
        text-align: center;
        padding-top: 80px;
        padding-bottom: 60px;
      }
      .dialog-btn {
        display: flex;
        align-items: center;
        justify-content: space-evenly;
        > div {
          width: 191px;
          height: 61px;
          line-height: 61px;
          background: #6f36d7;
          border-radius: 30px;
          text-align: center;
          font-family: "Anton";
          font-weight: 400;
          font-size: 24px;
          color: #ffffff;
          a {
            display: inline-block;
            width: 100%;
            height: 100%;
            color: #ffffff;
            text-decoration: none;
          }
        }
        .close {
          background: #57476c;
        }
      }
    }
  }
}
</style>