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

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

    tokenLoseflag = 0;
    public url = environment.url;

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

    public data = {
        getRealData: (data) => this.trans('post', '/data/getRealData', data),
        updateControl: (data) => this.trans('post', '/data/updateControl', 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();
    }
}
