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

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

    tokenLoseflag = 0;
    public url = environment.url;
    private load;
    private loadFlag = 1;

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

    public data = {
        getRealData: (data) => this.trans('post', '/data/getRealData', data),
        updateControl: (data) => this.trans('post', '/data/updateControl', data),
    };

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

    // MMP因为提前加载了内部的component,结果把里面的loading全加载出来了
    // 简直阿西了个巴巴, 老规矩,标志位,只能出现一个,出现多的但是还没有消失就不给
    async doLoading(msg) {
        if (this.loadFlag) {
            this.loadFlag = 0;
            this.load = await this.loading.create({
                spinner: 'circles',
                mode: 'ios',
                message: msg,
                duration: 90000
            });
            await this.load.present();
        }
    }

    async offLoading() {
        if (!this.loadFlag) {
            this.loadFlag = 1;
            await this.load.dismiss();
        } else {
            setTimeout(() => {
                this.offLoading();
            }, 3000);
        }
    }
}
