import { useBaseStoreWithOut } from "@/stores/modules/base";
import { useUserStoreWithOut } from "@/stores/modules/user";
import { computed, onMounted, ref } from "vue";
import { toast } from "@/utils/toast";
import { jumpOhterLink } from "@/utils";
import storage, { actions } from "@/utils/storage";
import http from "@/https";

export default function () {
  /**
   * 分享文案
   */
  const shareMessage = computed(() => {
    return `Receba ${200} BRL de graça,SAQUE RÁPIDO!`;
  });

  const platformConfig = computed(() => {
    return useBaseStoreWithOut().globalConfig.platformConfig || {};
  });

  /**
   * 分享列表
   */
  const shareList = computed(() => {
    return [
      // {
      //   icon: "icon_04",
      //   path: "",
      //   type: "xitong",
      // },
      {
        icon: "share/icon_1.png",
        path: `${platformConfig.value.web_fb_url}?u=${shareUrl.value}`,
        type: "facebook",
      },
      {
        icon: "share/icon_2.png",
        path: `${platformConfig.value.web_tg_url}?url=AmazingGame ${shareUrl.value}`,
        type: "tg",
      },
      {
        icon: "share/icon_3.png",
        path: `${platformConfig.value.web_whats_url}?text=AmazingGame ${shareUrl.value}`,
        type: "whatsapp",
      },
      {
        icon: "share/icon_4.png",
        path: `${platformConfig.value.web_X_url}text=AmazingGame&url=${shareUrl.value}`,
        type: "twitter",
      },
      // {
      //   icon: "share/icon_5.png",
      //   path: `https://t8club.com?uid=${useUserStoreWithOut().userInfo.uid}`,
      //   type: 'app'
      // },
      {
        icon: "share/icon_6.png",
        path: `${platformConfig.value.web_line_url}?url=${shareUrl.value}`,
        type: "line",
      },
    ];
  });
  /**
   * 分享链接
   */
  const url = computed(() => {
    return useBaseStoreWithOut().shareLinkUrl;
  });
  const shareUrl = computed(() => {
    if (useUserStoreWithOut().userInfo.uid) {
      return `${url.value}?u=${useUserStoreWithOut().userInfo.uid}`;
    }
    return url.value;
  });
  /**
   * 分享跳转方法
   * @param path
   * @param type
   * @returns
   */
  const goLink = (path: string, type: string) => {
    if (type == "xitong") {
      try {
        navigator.share({
          title: "webShare",
          text: "webShare",
          url: location.href,
        });
      } catch (error) {
        toast("share error");
      }
      return;
    }

    if (type == "facebook") {
      jumpOhterLink(path);
    } else {
      jumpOhterLink(path + " " + shareMessage.value);
    }
  };

  /**
   * 发送短信
   * @param mobile 手机号 号分割
   * @param body 发送内容
   */
  const onSendSms = (mobile: string, body: string) => {
    const browser = JSON.parse(
      <string>storage.getSessionStorage(actions.BROWSER)
    );
    if (!browser.isIos) {
      // sms:后面跟收件人的手机号,body后接短信内容
      // window.location.href='sms:188XXXXXXXX,134XXXXXXXX?body='+this.msg;
      window.location.href = `sms:${mobile}?body=${body}`;
    } else {
      // ios的写法
      // window.location.href='sms:/open?addresses=134XXXXXXXX,182XXXXXXXX&body='+this.msg;
      window.location.href = `sms:/open?addresses=${mobile}&body=${body}`;
    }
  };

  return {
    shareMessage,
    shareList,
    shareUrl,
    goLink,
    onSendSms,
  };
}
