import { parse } from 'query-string';
import Cookies from 'js-cookie';
import 'whatwg-fetch';
//单点登录地址
const LOGIN_URLS = {
	//测试内网
	'http://10.3.0.33:8080':
		'https://10.1.13.53:7002/casserver/login?service=http://10.3.0.33:8080/web/auth/',
	//测试外网
	'https://safeliab-test.ccic-net.com.cn':
		'https://ssotest.ccic-net.com.cn/casserver/login?service=https://safeliab-test.ccic-net.com.cn/web/auth/',
	//正式内网
	'http://safeliab.cloud.ccic-net.com.cn':
		'https://app.ccic-net.com.cn:27051/casserver/login?service=http://safeliab.cloud.ccic-net.com.cn/web/auth/',
	//正式外网
	'https://safeliab.ccic-net.com.cn':
		'https://sso.ccic-net.com.cn/casserver/login?service=https://safeliab.ccic-net.com.cn/web/auth/',
};

//登出地址
const LOGOUT_URLS = {
	//测试内网
	'http://10.3.0.33:8080':
		'https://10.1.13.53:7002/casserver/logout?service=http://10.3.0.33:8080/web/auth/',
	//测试外网
	'https://safeliab-test.ccic-net.com.cn':
		'https://ssotest.ccic-net.com.cn/casserver/logout?service=https://safeliab-test.ccic-net.com.cn/web/auth/',
	//正式内网
	'http://safeliab.cloud.ccic-net.com.cn':
		'https://app.ccic-net.com.cn:27051/casserver/logout?service=http://safeliab.cloud.ccic-net.com.cn/web/auth/',
	//正式外网
	'https://safeliab.ccic-net.com.cn':
		'https://sso.ccic-net.com.cn/casserver/logout?service=https://safeliab.ccic-net.com.cn/web/auth/',
};
window.onload = function () {
	const loca = window.location;
	const origin = loca.origin;
	const search = loca.search;
	const SERVICE_NAME = origin + '/web/auth/';
	const LOGIN_URL = LOGIN_URLS[origin];
	const LOGOUT_URL = LOGOUT_URLS[origin];
	const ROOT_URL = '/web/';
	const params = parse(search);
	const ticket = params.ticket;
	const logoutType = params.logoutType;
	function logoutOut() {
		Cookies.remove('insure-token', { path: '/' });

		Cookies.remove('expert-token', { path: '/' });

		Cookies.remove('enterprise-token', { path: '/' });

		window.localStorage.clear();

		const loca = window.location;

		loca.replace(LOGOUT_URL);
	}

	function checkStatus(response) {
		if (response.status >= 200 && response.status < 300) {
			return response;
		} else {
			var error = new Error(response.statusText);
			error.response = response;
			throw error;
		}
	}
	function parseJSON(response) {
		return response.json();
	}

	if (logoutType === 'manual') {
		logoutOut();
		return;
	}
	if (ticket) {
		window
			.fetch(
				'/base/singleLogin/auth?ticket=' + ticket + '&service=' + SERVICE_NAME,
			)
			.then(checkStatus)
			.then(parseJSON)
			.then(
				(result) => {
					if (result.code == 0 && result.success) {
						if (result.data.token && result.data.userType) {
							var keys = {
								1: 'enterprise',
								3: 'insure',
								5: 'expert',
							};
							Cookies.set(
								keys[result.data.userType] + '-token',
								result.data.token,
								{ expires: 1, path: '/' },
							);

							window.location.href =
								ROOT_URL + keys[result.data.userType] + '/app';
						} else {
							logoutOut();
						}
					} else {
						logoutOut();
					}
				},
				() => {
					logoutOut();
				},
			);
	} else {
		window.location.href = LOGIN_URL;
	}
};