import Vue from "vue";
import { getLatestBlock } from "ycc-api/dist/cmjs/service/home/index";
import { iBlockMsg } from "ycc-api/dist/cmjs/service/home/types";
import { getRpc } from "ycc-api/src/service/Rpc";
import Base64 from "@/utils/_base64";
export default Vue.extend({
  data() {
    return {
      latestHeight: NaN,
      totalTx: NaN,
      value: "",
      latestBlocks: [] as iBlockMsg[],
      latestTxs: [
        {
          hash: "lkajskfdkasdf",
          from: "laksjdflas",
          to: "lakdjflkasdf",
          time_block: 1643167975993,
          amount: 30000000,
        },
        {
          hash: "lkajskfdkasdf",
          from: "laksjdflas",
          to: "lakdjflkasdf",
          time_block: 1643167975993,
          amount: 30000000,
        },
        {
          hash: "lkajskfdkasdf",
          from: "laksjdflas",
          to: "lakdjflkasdf",
          time_block: 1643167975993,
          amount: 30000000,
        },
      ],
    };
  },
  mounted() {
    this.init();
  },
  methods: {
    setValue(v: string) {
      this.value = v;
    },
    clickSearch(value: string) {
      console.log(value, "show value");
    },
    getLatestBlockData() {
      this.latestBlocks = (" "
        .repeat(5)
        .split(" ")
        .map((i) => ({})) as unknown) as iBlockMsg[];
      return getLatestBlock("ycc", "/yccApi").then((ret) => {
        this.latestBlocks = ret || [];
        return ret;
      });
    },
    hexCharCodeToStr(hexCharCodeStr: string) {
      const num = hexCharCodeStr;
      const trimedStr = num.trim();
      const rawStr =
        trimedStr.substr(0, 2).toLowerCase() === "0x"
          ? trimedStr.substr(2)
          : trimedStr;
      const len = rawStr.length;
      if (len % 2 !== 0) {
        alert("Illegal Format ASCII Code!");
        return "";
      }
      let curCharCode;
      const resultStr = [];
      for (let i = 0; i < len; i = i + 2) {
        curCharCode = parseInt(rawStr.substr(i, 2), 16); // ASCII Code Value
        resultStr.push(String.fromCharCode(curCharCode));
      }
      //转码ASCII——base64
      const b = new Base64();
      const str = b.encode(`TotalFeeKey:${resultStr.join("")}`);
      return str;
    },
    async init() {
      this.getLatestBlockData();

      const lastHeaderRet = await getRpc("/yccApi").getLastHeader();
      if (!lastHeaderRet.error) {
        this.latestHeight = lastHeaderRet.result.height;
        this.$store.commit("saveMaxHeight", this.latestHeight);

        const str = this.hexCharCodeToStr(lastHeaderRet.result.hash);
        const totalFeeRet = await getRpc("/yccApi").queryTotalFee([str]);
        if (!totalFeeRet.error) {
          this.totalTx = totalFeeRet.result.txCount;
        }
      }
    },
  },
  computed: {
   
    msgList(): any[] {
      return [
        {
          icon: require("@/assets/images/blockChainBrowser/home/latestBlock.png"),
          title: this.$t("lang.chainMsg.latestBlock"),
          number: this.latestHeight,
        },
        {
          icon: require("@/assets/images/blockChainBrowser/home/nodeNumber.png"),
          title: this.$t("lang.chainMsg.nodeNum"),
          number: 10012,
        },
        {
          icon: require("@/assets/images/blockChainBrowser/home/circulateNumber.png"),
          title: this.$t("lang.chainMsg.circulation"),
          number: 102938,
        },
        {
          icon: require("@/assets/images/blockChainBrowser/home/pledgeQuantity.png"),
          title: this.$t("lang.chainMsg.pledgeQuantity"),
          number: 102938,
        },
      ];
    },
  },
});
