import https from "@/https";
import { showLoading, closeLoading, toast } from "@/utils/toast";
import { inject } from "vue";
import storage, { actions } from "@/utils/storage";
import { useUserStore } from "@/stores/modules/user";
import { useRouter } from "vue-router";
import socketApi from "@/socket/socketApi";
import { getLocationParams } from "@/utils";
import { useGiftStore } from "@/stores/modules/gift";
import { GOOGLE_USER_INFO } from "@/common/config";
import axios from "axios";
import { googleTokenLogin } from "vue3-google-login";

interface LoginProps {
  mobile: string;
  pwd: string;
  code: number;
}

export default function () {
  const cookies: any = inject("$cookies");
  const socket: any = inject("socket");
  const userStore = useUserStore();
  const giftStore = useGiftStore();
  const router = useRouter();

  const onLogin = async (data: LoginProps, status?: boolean) => {
    const params = {
      mobile: data.code + data.mobile,
      pwd: data.pwd,
    };
    const res: ResultData = await https.login(params);
    if (res.code !== 0) {
      toast(res.txt);
      return;
    }
    storage.setStorage(actions.USER_INFO, JSON.stringify(res.data));
    storage.setStorage(actions.USER_TOEKN, res.data.token);
    userStore.setUserInfo(res.data);
    window.sessionStorage.setItem("showOne", '2');
    connectSocket(res.data, status);
  };

  const connectSocket = (data: any, status?: boolean) => {
    socket.disconnect();
    socket.connect().then(() => {
      console.log("login connect socket");
      const lp = getLocationParams();
      // if (!data.inviteUid && lp.u) {
      //   acceptInvite(lp.u);
      // }

      if (!userStore.userInfo.mobile) {
        router.replace("/bind-phone");
      } else if (status) {
        router.replace("/");
      } else {
        router.replace("/");
      }
      giftStore.getThreeInOneInfo();
      userStore.getUserInfo();
      // storage.setSessionStorage(actions.DRAW_POPUP, 0);
      closeLoading();
    });
  };

  const googleLogin = () => {
    googleTokenLogin().then(async (response: any) => {
      axios
        .get(GOOGLE_USER_INFO, {
          params: { access_token: response.access_token },
        })
        .then(async (res: any) => {
          showLoading();
          let params: any = {
            google: res.data.sub,
          };
          let uid = getLocationParams(window.location.href);
          if (uid.u) {
            params.inviteUid = uid.u;
          }
          const { code, data, txt }: ResultData = await https.googleLogin(
            params
          );
          if (code !== 0) {
            toast(txt);
            closeLoading();
            return;
          }
          storage.setStorage(actions.USER_INFO, JSON.stringify(data));
          storage.setStorage(actions.USER_TOEKN, data.token);
          userStore.setUserInfo(data);
          connectSocket(data);
        })
        .catch((error) => {
          console.log(error);
        });
    })
  }

  const acceptInvite = (uid: number) => {
    socketApi.acceptInvite(Number(uid));
  };

  return {
    onLogin,
    connectSocket,
    googleLogin,
  };
}
