const {
  BrowserWindow,
  ipcMain,
  session,
  Notification,
  remote
} = require("electron");
const path = require("path");

module.exports = () => {
  let win;
  const url = "https://csdn.net";

  let init = mainWindow => {
    ipcMain.on("token", (e, info) => {
      console.log("on token");

      const loginNote = createNote("登录成功");

      session.defaultSession.cookies
        .get({
          url: url
        })
        .then((cookies, err) => {
          const userInfo = {};
          cookies.forEach(cookie => {
            userInfo[cookie.name] = decodeURIComponent(cookie.value);
          });
          userInfo.UserAvatar = decodeURIComponent(info.UserAvatar);
          console.log(userInfo);
          global.opConfig.set("userInfo", userInfo);
          mainWindow.webContents.send("setUserInfo", userInfo);

          win.close();
          loginNote.show();
        });
    });
    ipcMain.on("logout", (e, info) => {
      console.log("remove cookie");
      const logoutNote = createNote("退出登录");
      session.defaultSession.cookies
        .get({
          url: url
        })
        .then((cookies, err) => {
          cookies.forEach(cookie => {
            session.defaultSession.cookies.remove(url, cookie.name, e => {});
          });
          mainWindow.webContents.send("setUserInfo", "");
          logoutNote.show();
        });
    });
  };

  let createNote = note => {
    return new Notification({
      title: "CSDN通知", // 通知标题
      icon: path.resolve(__static, "./icon.png"),
      body: note, // 内容
      href: "https://www.csdn.net/"
    });
  };

  let createWindow = () => {
    console.log("create window");
    session.defaultSession.cookies
      .get({
        url: url
      })
      .then((cookies, err) => {
        console.log("cookies", cookies);
      });

    win = new BrowserWindow({
      frame: true,
      autoHideMenuBar: true,
      title: false,
      width: 460,
      height: 400,
      show: false,
      resizable: false,
      closable: true,
      titleBarStyle: "hiddenInset",
      minimizable: true,
      alwaysOnTop: true,
      webPreferences: {
        webSecurity: false,
        enableRemoteModule: true,
        nodeIntegration: true,
        backgroundThrottling: false,
        devTools: false,
        webviewTag: true,
        preload: `${path.resolve(__static, "./login_preload.js")}`
      }
    });
    // win.loadFile(path.resolve(__dirname, "../../../../send.html"));
    // win.loadURL("https://meiyin.xyz");
    win.loadURL(
      `https://passport.csdn.net/account/login?from=https://gitcode.net/about/quicker`
    );
    win.on("closed", () => {
      win = undefined;
    });
    // 打包后，失焦隐藏
    win.on("blur", () => {
      // win.hide();
    });
    win.webContents.on("did-stop-loading", () => {
      win.webContents.insertCSS(
        `
        #csdn-toolbar, #copyright-box, #app .notes, #app .icon-list, #app .user-service-link { display: none }
        #app, #app .container-main { min-width: unset!important; min-height: unset!important; }
        #app .main { width: unset }
        html, body { overflow: hidden }
        #app .main-login { position: fixed; top: 0; left: 0; width: 100%; height: 100%; z-index: 999 }
        #app .form-control-phone { width: 62%; float: right }
        #app .main-process-login { width: 66%; margin-top: 20px }
        .login-success-tip, .quicker_loading { position: fixed; width: 100%; height: 100%; left: 0; top: 0; color: #fff; z-index: 2000; background: #fff }
        .login-success-tip::after, .quicker_loading::after { content: '正在同步个人设置…'; color: #666; position: absolute; background: #fff; left: 50%; top: 50%; transform: translate(-50%, -50%); }
        .quicker_loading { height: 100vh; }
        `
      );
      win.openDevTools()
      win.show();
    });
  };

  let getWindow = () => win;

  return {
    init,
    getWindow,
    createWindow
  };
};
