<template>
  <div class="chain-modal" v-if="props.visible" :class="{ mobile: 'mobile' }">
    <div class="modal-bg" @click="cancelSelect"></div>
    <div class="modal-content" :class="{ mobile: 'mobile' }">
      <div class="modal-title">
        <div>{{title||'Please connect your wallet'}}</div>
        <img
          src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAfCAYAAACGVs+MAAAAAXNSR0IArs4c6QAAApVJREFUWEe1lj1v01AUht9zEygDA0tYYAIJVWJL3KGtUZyFAQYWggQSqkgkhuQPMDDDHwBRYKIoHaJOSIgBNUlxUoY4KQtiRvwCFopEfA+yG4fE9ce1ayJPubrnffyeDx8CgE8d62ZO0GMiOM+T1XLxnfN/1r/BrnUdOXrkxGXIp7qx8pFMc3gpJ8U3ZpyeCtoAPVg3im+zBBh0h7dBYhvAqWnc37Bpmfp71j1iavnEbEmydq28spUFRID4UVimu2SaX64I2/4KIP8/IELFAZvz+avkiO7vjWvM/BqAyBIiQpyZuaFXtE0XwIXojutM/CoriChxgJvrhvbC0Z0BZAmhKn4MIAuIJOKBACeBSCoeCuAc9LvDOpEIrAkiqq2Viwstau5aVZFz29nrc6+8eD7n/rZeqAH/oSpEWvFIBzyYOAh7Ig/TvLkXP9IBFQgAMsh2wdRYrRQ34yapEkBMTfg1WFVcKQXz0SPSMSu4JOKJAZwLg97oJYCHQdYSaGvNKG7E2T5/rpwC55LZs6oC2AbI/+HyYtpBLRoFpAygID6DYOa6XtHeqDihBBAhzu5yE/AVVYWIBYgSdwrOhv0nbGKqQEQC7PcOqgw7KOcLrRY1rOIgQgHCxIlISomm7hsyaSECAZKKq0zMMCeOAaQVTwuxuBGF5DzM9rA2i9oxmbiul/+16Ayg3xndIcEt/5BJKu5BqUK4AIPO8BaE2PGv5mnFlSAYG3ql1DoC6Fk/ALq4MKPdapdNZ3VWmWgp0vHzXOHMeWJmGvTGh0RYmi0JGYnHOMH5yaQwdWD0HEBjekEy84nf3O+IvyYY+KAbpRsuQLvdzl0oXL4P0DITv9eNknkS28Pufu4eGJJkFcD3pbP8TNO0X38BHAnKrhM25C0AAAAASUVORK5CYII="
          alt="close"
          class="close"
          @click="cancelSelect"/>
      </div>
      <div class="modal-sub-title">
        {{subTitle || 'Connect your wallet to start your Counter Fire journey'}}
      </div>
      <div class="modal-body">
        <div
          class="chain-modal-card"
          v-for="data in currentDatas"
          :key="data.name"
          @click="cardClicked(data.id)"
        >
          <div class="icon">
            <img :src="data.logo" :alt="data.name" />
          </div>
          <div class="name">{{ data.name }}</div>
          <div class="tip">{{ data.tip }}</div>
          <div class="fire-wallet" v-if="data.id == 5 && isFireWallet">
            <div class="fire-wallet-item" @click="googleLogin">
              <div class="icon">
                <img :src="icon_google" alt="">
              </div>
              <div class="name">Google</div>
            </div>
            <div class="fire-wallet-item" @click="appleLogin">
              <div class="icon">
                <img :src="icon_apple" alt="">
              </div>
              <div class="name">Apple</div>
            </div>
          </div>
        </div>
      </div>
      
    </div>
  </div>
</template>
<script setup>
import { computed, ref } from "vue";
import { ALL_PROVIDERS, icon_google, icon_apple } from "@/configs/configchain";
import { allProviders } from "@/components/chain/BlockChain"
import { getlogin } from "@/api/User";

import {apiCecInfo} from "@/utils/marketplace"
import { useMarketplaceStore } from "@/store/marketplace"
import {} from "@/store/wallet"
const marketplaceStore = useMarketplaceStore()

const props = defineProps({
  visible: ref(Boolean),
  close: Function,
  title: ref(String),
  subTitle: ref(String),
  initData: Object,
  disabled: Array
});

const currentDatas = computed(() => {
  // debugger
  let list = ALL_PROVIDERS.map((item) => {
    const Provider = allProviders[item.id];
    // console.log(Provider)
    const installed = new Provider().installed;
    // console.log(installed)
    const initData = props.initData;
    let current = null;
    if (initData) {
      current = initData[item.id];
    }
    const tip = installed ? current?.tip || item.tip || '' : "Click to Install";
    return {
      id: item.id,
      name: item.name,
      logo: item.logo,
      installed,
      downloadUrl: item.downloadUrl,
      tip,
    };
  });
  const disabled = props.disabled || [];
  list = list.filter((item) => !disabled.includes(item.id));
  return list;
});

async function selectWallet(id) {
  const Provider = allProviders[id];
  const walletInstance = new Provider()
  const { provider, accounts } = await walletInstance.web3Provider();
  console.log(accounts)
  hideModal({errcode: 0, provider, wallet: id, walletInstance, accounts});
}

const isFireWallet = ref(false)
async function cardClicked(id) {
  if(id == 5) {
    isFireWallet.value = true
  } else {
    const data = currentDatas.value.find((item) => item.id === id);
    if (!data.installed) {
      // return
      // // open download page
      if (!data.downloadUrl) {
        return;
      }
      window.open(data.downloadUrl, '_blank');
    } else {
      await selectWallet(id);
    }
  }
}

const walletTake = import.meta.env.VUE_APP_WALLET_TAKE
const googleLogin = async () => {
  // return
  let res = await callMethod("walletLogin", "0", walletTake);
  res = JSON.parse(res);
  marketplaceStore.fireAddress = res.data.address
  marketplaceStore.fireToken = res.data.token + '.cf'
  hideModal({errcode: 0})

    // let token = 'eyJhbGciOiJFZERTQSIsInR5cCI6IkpXVCJ9.eyJpZCI6IjY1YjBjMmM4ZDY3MTBhYmQ1YjFhN2RhMyIsInVpZCI6IiIsImdpZCI6IiIsIm9wZW5pZCI6IjEwODc4Nzc3NTE0NTY3NzY2NzkwMCIsInZlcnNpb24iOjAsInBsYXQiOjAsImlhdCI6MTcyNDMyODg1NSwiZXhwIjoxNzI0NDE1MjU1fQ.1Qg06-q7t9YBgteWL4DtXphaHb9qlWbNJfIObdftqdO6uR2Qw5XTaZZoqdBJM8htjXpeKiYWD44ddTuMEVTHCA'
    // try{
    //   let data = await apiCecInfo('0x50f82c3e944f92cbbc1d8bfc601fc3fc5f7ab155', res.data.token+'.cf')
    //   console.log(data)
    // } catch (e) {
    //   console.log(e)
    // }
}
const appleLogin = async () => {
  let res = await callMethod("walletLogin", "1", walletTake);
    res = JSON.parse(res);
}

function cancelSelect() {
  hideModal({errcode: 1, errmsg: "user cancel select wallet"});
}

function hideModal(result = null) {
  props.close(result);
}
</script>
<style lang="scss" scoped>
.chain-modal {
  position: fixed;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  overflow: auto;
  margin: 0;
  z-index: 10001;
  .modal-bg {
    position: absolute;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    overflow: auto;
    margin: 0;
    background-color: #000000a3;
  }
  .modal-content {
    width: 31.25vw;
    background-color: #2d2738;
    border-radius: 20px;
    color: white;
    margin: auto;
    display: flex;
    position: relative;
    margin-top: 15vh;
    flex-direction: column;
    .mobile {
      width: 100vw;
    }
    .modal-title {
      border-radius: 12px 12px 0 0;
      padding: .78125vw 1.5625vw;
      // border-bottom: 1px solid rgba(195, 195, 195, 0.14);
      display: flex;
      flex-direction: row;
      align-items: center;
      justify-content: space-between;
      font-size: 1.35417vw;
      height: 3.125vw;
      background-color: #1c1725;
      .close{
        cursor: pointer;
      }
    }
    .modal-sub-title {
      padding: .78125vw 1.5625vw;
      // border-bottom: 1px solid rgba(195, 195, 195, 0.14);
      font-size: 16px;
      font-weight: 400;
    }
    .modal-body {
      padding: 0 1.5625vw 1.04167vw;
      .chain-modal-card {
        transition: background-color 0.2s ease-in-out 0s;
        display: flex;
        flex-direction: row;
        -webkit-box-pack: center;
        justify-content: center;
        -webkit-box-align: center;
        align-items: center;
        padding: .78125vw 1.5625vw;
        text-align: center;
        cursor: pointer;
        border: .10417vw solid #625a6b;
        border-radius: .41667vw;
        padding: .78125vw 1.5625vw;
        margin: 1.04167vw 0;
        position: relative;
        // &:first-child {
        //   border-radius: 12px 12px 0 0;
        // }
        // &:not(:first-child) {
        //   border-top: 1px solid rgba(195, 195, 195, 0.14);
        // }
        // &:last-child {
        //   border-radius: 0 0 12px 12px;
        // }
        .icon {
          // width: 45px !important;
          // height: 45px !important;
          display: flex;
          border-radius: 50%;
          overflow: visible;
          box-shadow: none;
          -webkit-box-pack: center;
          justify-content: center;
          -webkit-box-align: center;
          align-items: center;
          img {
            width: 45px;
            height: 42px;
          }
          .icon-svg {
            width: 100%;
            height: 100%;
            fill: currentColor;
            color: unset;
            stroke: none;
          }
        }
        .name {
          width: 60%;
          font-size: 24px;
          font-weight: 700;
          text-align: left;
          margin-left: 20px;
        }
        .tip {
          width: 30%;
          text-align: right;
          color: red;
        }
        .tip a{
          color: red;
        }
        .fire-wallet {
          position: absolute;
          top: 73px;
          width: 100%;
          height: 100%;
          background: #2a2b3e;
          border-radius: .41667vw;
          z-index: 9;
          .fire-wallet-item {
            height: 70px;
            padding: 10px 20px;
            background: #D5BEFA;
            border-bottom: 2px solid #1E1925;
            padding: .78125vw 1.5625vw;
            border-radius: .41667vw;
            display: flex;
            .name {
              margin-left: 20px;
            }
            &:last-child {
              border: 0px;
            }
          }
        }
      }
      .chain-modal-card:hover {
        background-color: #D5BEFA;
        color: #1E1925;
      }
    }
    
  }
}
</style>
