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


export const checkUnique = (params: any) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_CHECK_UNIQUE, method: 'POST', data: params
        }).then(res => {
            resolve(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 uploadPerm = (params: any) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_USER_PERMS, method: 'POST', data: { ...params }//{ push_enabled: push_enable }
        }).then(res => {
            resolve(res)
            // dispatch(loginSuccess(res));
        }).catch(e => {
            reject(e)
        })
    })
}

export const getPerm = (params: any) => {
    return new Promise((resolve, reject) => {
        request({
            url: API_USER_PERMS, method: 'GET', data: { ...params }//{ push_enabled: push_enable }
        }).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 = () => {

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

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

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

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

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)
        })
    })
}

export const clientId = (isJiguang = true) => {
    if (isJiguang) {
        if (global.registerID) {
            request({
                url: API_CLIENT_ID, method: 'POST', data: {
                    provider: 'JIGUANG',
                    client_type: kIsIOS ? 'IOS' : 'ANDROID',
                    client_id: global.registerID
                }
            })
        }
    }
    else {
        if (global.oneID) {
            request({
                url: API_CLIENT_ID, method: 'POST', data: {
                    provider: 'ONESIGNAL',
                    client_type: kIsIOS ? 'IOS' : 'ANDROID',
                    client_id: global.oneID
                }
            })
        }
    }


}

