import Taro from '@tarojs/taro'
import { API_OAUTH_LOGIN, API_REGISTER, API_LOGIN, API_LOGOUT, API_CLEAR_USER, API_USER_INFO, API_CHECK_UNIQUE } from './http/api'
import { request } from './http/request'
import { clearSuccess, getInfoSuccess, loginSuccess, logoutSuccess, registerSuccess, updateSuccess } from '@/store/user'


export const checkUnique = (username?: string, email?: string) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_CHECK_UNIQUE, method: 'GET', data: { username: username, email: email }
        }).then(res => {
            resolve(res);
            console.log(res);
            // dispatch(loginSuccess(res));
        })
    })

}

export const login = (username: string, password: string) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_LOGIN, method: 'POST', data: { username, password }
        }).then(res => {
            resolve(res)
            // dispatch(loginSuccess(res));
        }).catch(e => {
            reject(e)
        })
    })
}

export const wxLogin = (code: string/*, encryptedData: string, iv: string*/) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_OAUTH_LOGIN, method: 'POST',
            data: {
                code: code,
                type: 'WX_MP',
                app_version: '1',
                client_version: '1',
                client_type: 'WX_MP',
                // extra: {
                //     encryptedData: encryptedData,
                //     iv: iv
                // }
            }
        }).then(res => {
            resolve(res)
            // dispatch(loginSuccess(res));
        }).catch(e => {
            reject(e)
        })
    })
};

/*
export const wxLogin = (code: string) => (dispatch: any) => {
    
    request({
        url: API_OAUTH_LOGIN, method: 'POST', data: { code,type:'WX_MP',app_version:'1',client_version:'1',client_type:'WXP_MP' }
    }).then(res => {
        console.log(res);
        dispatch(loginSuccess(res));
    })
}*/

export const register = (name: string, email: string, password: string) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_REGISTER, method: 'POST', data: { client_type: 'IOS', username: name, email: email, password: password }
        }).then(res => {
            resolve(res);
            // dispatch(loginSuccess(res));
        }).catch(e => {
            reject(e)
        })
    })
}

export const logout = () => (dispatch: any) => {
    request({
        url: API_LOGOUT, method: 'GET', data: {}
    }).then(_ => {
        dispatch(logoutSuccess());
        Taro.navigateBack();
    })
}

export const clear = () => (dispatch: any) => {
    request({
        url: API_CLEAR_USER, method: 'DELETE', data: {}
    }).then(_ => {
        dispatch(clearSuccess());
        Taro.navigateBack();
    })
}

export const getInfo = () => {
    return new Promise((resolve, reject) => {
        request({
            url: API_USER_INFO, method: 'GET', data: {}
        }).then(res => {
            return resolve(res)
            // dispatch(getInfoSuccess(res));
        }).catch(e=>{
            return reject(e)
        })
    })
    
}


export const update = (params: any) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_USER_INFO, method: 'POST', data: params
        }).then(res => {
            // dispatch(updateSuccess(params));
            resolve(res)
        }).catch(e=>{
            reject(e)
        })
    })
}

