import store from '../store';
import { checkLogin } from './login';
import { login } from '../api/public';
import Cache from '../utils/cache';
import { STATE_R_KEY, USER_INFO, EXPIRES_TIME, LOGIN_STATUS} from './../config/cache';

class Routine 
{
	
	constructor() 
	{
	    this.scopeUserInfo = 'scope.userInfo';
	}
	
	async getUserCode(){
		let isAuth = await this.isAuth(), code = '' ;
		console.log(isAuth,"isAuth++++++++++")
		if(isAuth)
			code = await this.getCode();
		console.log(code,'code----------')
		return code;
	}
	
	/**
	 * 获取用户信息
	 */
	async getUserInfo(){
		let  that = this , code =await that.getUserCode();
		console.log(code,'code+++++++')
		return new Promise( (resolve,reject) => {
			// console.log(code);
			uni.getUserInfo({
				// withCredentials:true,
				// #ifndef MP-TOUTIAO
				lang: 'zh_CN',
				// #endif
				success(user) {
					// console.log(user);
					if(code) user.code = code;
					// console.log(user.rawData);
					resolve({userInfo:user,islogin:false});
				},
				fail(res){
					console.log(res,"getUserInfo--------err")
					reject(res);
				}
			})
		})
	}
	
	/**
	 * 获取用户信息
	 */
	authorize()
	{
		let that = this;
		return new Promise((resolve,reject)=>{
			if(checkLogin())
				return resolve({
					userInfo:Cache.get(USER_INFO,true),
					islogin:true,
				});
			uni.authorize({
			    scope: that.scopeUserInfo,
			    success(res) {
					console.log(res,'uni.authorize++++++++++');
					// #ifdef MP-TOUTIAO
					console.log(res,'uni.authorize++++++++++');
					resolve({islogin:false},res.data);
					// #endif
					// #ifdef MP-WEIXIN
					resolve({islogin:false});
					// #endif
			    },
				fail(res){
					console.log('uni.authorize-------------')
					reject(res);
				}
			})
		})
	}
	
	async getCode(){
		let provider = await this.getProvider();
		return new Promise((resolve,reject)=>{
			if(Cache.has(STATE_R_KEY)){
				console.log(Cache.has(STATE_R_KEY),'有STATE_R_KEY')
				return resolve(Cache.get(STATE_R_KEY));
			}
			uni.login({
				provider:provider,
				success(res) {
					if (res.code) Cache.set(STATE_R_KEY, res.code ,10800);
					return resolve(res.code);
				},
				fail(){
					return reject(null);
				}
			})
		})
	}
	
	/**
	 * 获取服务供应商
	 */
	getProvider()
	{
		return new Promise((resolve,reject)=>{
			uni.getProvider({
				service:'oauth',
				success(res) {
					resolve(res.provider);
				},
				fail() {
					resolve(false);
				}
			});
		});
	}
	
	/**
	 * 是否授权
	 */
	isAuth(){
		let that = this;
		return new Promise((resolve,reject)=>{
			uni.getSetting({
				success(res) {
					console.log(res,'++++++++++是否授权+++++++++++')
					if (!res.authSetting[that.scopeUserInfo]) {
						resolve(true)
					} else {
						resolve(true);
					}
				},
				fail(){
					 resolve(false);
				}
			});
		});
	}
	getUserProfile(code) {
		return new Promise((resolve, reject) => {
			uni.getUserProfile({
				lang: 'zh_CN',
				desc: '用于完善会员资料', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
				success(user) {
					if (code) user.code = code;
					resolve({
						userInfo: user,
						islogin: false
					});
				},
				fail(res) {
					reject(res);
				}
			})
		})
	}
	authUserInfo(data)
	{
		return new Promise((resolve, reject)=>{
			login(data).then(res=>{
				console.log("login",res)
				let time = res.data.expires_time - Cache.time();
				store.commit('UPDATE_USERINFO', res.data.user);
				store.commit('LOGIN', {token:res.data.token, time:time});
				store.commit('SETUID', res.data.user.uid);
				Cache.set(EXPIRES_TIME,res.data.expires_time,time);
				Cache.set(USER_INFO,res.data.userInfo,time);
				return resolve(res);
			}).catch(res=>{
				console.log("7777777777777")
				return reject(res);
			})
		})
	}
}

export default new Routine();