import { HttpService } from './http.service';
import { Injectable } from '@angular/core';
import { Router } from '@angular/router';
import { environment } from '../../environments/environment';
import { ToastController } from '@ionic/angular';

@Injectable()
export class ApiService {
    constructor(private http: HttpService, private router: Router, private toast: ToastController) {
    }

    tokenLoseflag = 0;
    public url = environment.url;

    public login = {
        dologin: (data) => this.trans('post', '/login/dologin', data),
        logout: (data) => this.trans('get', '/login/logout', data),
    };

    public user = {
        addUser: (data) => this.trans('post', '/user/addUser', data),
        deleteUser: (data) => this.trans('post', '/user/deleteUser', data),
        getAll: (data) => this.trans('get', '/user/getAll', data),
        getByUserNameForLike: (data) => this.trans('post', '/user/getByUserNameForLike', data),
        getSelfInfo: (data) => this.trans('get', '/user/getSelfInfo', data),
        updateSelfPassword: (data) => this.trans('post', '/user/updateSelfPassword', data),
        updateSelfUser: (data) => this.trans('post', '/user/updateSelfUser', data),
        updateUser: (data) => this.trans('post', '/user/updateUser', data),
    };

    private trans(type, addr, data) {
        this.sendHttpReq(type, addr, data[0], data[1], data[2], data[3]);
    }

    private sendHttpReq(type, addr, reqB, next, error, complete) {
        let nextFunc;
        if (addr === '/login/dologin') {
            nextFunc = (data: any) => {
                next(data);
            };
        } else {
            nextFunc = (data: any) => {
                if (this.http.handleToken(data)) {
                    next(data);
                } else {
                    if (!this.tokenLoseflag) {
                        this.presentMsgToast('登录失效,请重新登陆');
                        this.http.toLoginPage(2000);
                        this.tokenLoseflag = 1;
                        setTimeout(() => {
                            this.tokenLoseflag = 0;
                        }, 5000);
                    }
                }
            };
        }
        if (!error) {
            error = (err) => {
                let data;
                if (err && err.error && err.error.status) {
                    if (err.error.status === 401) {
                        data = {code: 0, message: '用户尚未登录'};
                        this.router.navigate(['/extra']);
                    } else {
                        data = {code: 0, message: '服务器异常'};
                    }
                } else {
                    data = {code: 0, message: '服务器异常'};
                }
                next(data);
            };
        }
        if (type === 'get') {
            this.http.get(this.url + addr, nextFunc, error, complete);
        } else {
            this.http.post(this.url + addr, reqB, nextFunc, error, complete);
        }
    }

    async presentMsgToast(msg) {
        const toast = await this.toast.create({
            message: msg,
            duration: 2000,
            mode: 'ios',
            cssClass: 'msgToast'
        });
        toast.present();
    }

    async presentBtnToast(msg, btn, position) {
        const toast = await this.toast.create({
            message: msg,
            showCloseButton: true,
            position: position,
            closeButtonText: btn,
            mode: 'ios',
            cssClass: 'btnToast'
        });
        toast.present();
    }
}
