import {LoadingController, AlertController, ToastController} from "@ionic/angular";
import {Injectable} from '@angular/core';
import {Http, Headers} from '@angular/http';
import {Observable} from "rxjs";
import {environment} from "../environments/environment";

@Injectable()
export class AppGlobal {
    //缓存key的配置
    static cache: any = {
        slides: "_dress_slides",
        categories: "_dress_categories",
        products: "_dress_products"
    };

    //接口基地址
    static domain = "http://101.89.112.92:80";       //正式环境
    // static domain = "http://180.168.156.212:2931";   //测试环境
    // static domain = "";   //本地环境


    //图片地址
    //  static picture = environment.domain + "/wisdomgroup/manager/getIcon/";     //正式地址
    static picture = environment.domain + "/wisdomgroup/manager/getIcon/";   //测试

    //通知公告图片参数
    //  static pictureNotice = environment.domain;    //正式地址
    static pictureNotice = environment.domain;  //测试

    //接口地址
    static API: any = {
        getCategories: '/api/ionic3/getCategories',
        getLogin: '/app/loginpost',
        getDetails: '/api/ionic3/details'
    };
}

@Injectable()
export class AppService {
    constructor(public http: Http) {
    }

    // 对参数进行编码
    encode(params, flag) {
        var str = '';
        if (params) {
            if (flag == 'get') { //get   /a/b
                for (var key in params) {
                    if (params.hasOwnProperty(key)) {
                        var value = params[key];
                        str += value + '/';
                    }
                }
                str = '/' + str.substring(0, str.length - 1);
            }
            if (flag == 'post') { //post  a=b&c=d
                for (var key in params) {
                    if (params.hasOwnProperty(key)) {
                        var value = params[key];
                        str += key + '=' + value + '&';
                    }
                }
                str = str.substring(0, str.length - 1);
            }
        }
        return str;
    }

    //get请求
    ObserverHttpGet(url, params): Observable<any> {
        return this.http.get(environment.domain + url + this.encode(params, "get"))    //app
    }


    //get请求
    ObserverHttpGetData(url, params): Observable<any> {
        return this.http.get(environment.domain +url, params)   //本地
    }

    //get请求
    ObserverHttpGetAdd(url, params): Observable<any> {
        return this.http.get(environment.domain + url + params)    //app
    }

    //get请求带？的
    ObserverHttpGetOption(url, params): Observable<any> {
        return this.http.get(environment.domain + url, {params: params});    //app
    }


    //get请求带pararms ,body 的
    ObserverHttpGetAddBody(url, params,body): Observable<any> {
        return this.http.post(environment.domain + url + params,null, {      //本地
            params:body,
            headers: new Headers({
                // "Accept": "application/json",
                "Content-Type": "application/json"
                // 'Content-Type':'application/x-www-form-urlencoded,charset=UTF-8'
            })
        })
    }


    //delete
    ObserverHttpDetelete(url, params): Observable<any> {
        return this.http.delete(environment.domain + url + this.encode(params, "get"))    //app
    }

    //delete
    ObserverHttpDeteleteOption(url, params): Observable<any> {
        return this.http.delete(environment.domain + url + params)    //app
    }

    //delete
    ObserverHttpDeteleteData(url, params, data): Observable<any> {
        return this.http.delete(environment.domain + url + params, {    //app
            params: this.encode(data, 'post'),
            headers: new Headers({
                "Content-Type": "application/json"
            })
        })   //本地
    }

    ObserverHttpPostData(url, params) {
        return this.http.post(environment.domain + url, params, {      //app
            headers: new Headers({
                "Content-Type": "application/json"
            })
        })
    }

    ObserverHttpPostAdd(url, params) {
        return this.http.post(environment.domain + url + params, {      //app
            headers: new Headers({
                "Content-Type": "application/json"
            })
        })
    }

    ObserverHttpPostOption(url, data) {
        return this.http.post(environment.domain + url, null, {      //app
            params: data,
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded,charset=UTF-8'
            })
        });

    }

    //post请求
    ObserverHttpForm(url, params,body) {
        return this.http.post(environment.domain+url + params,null,{
            params:body,
            headers: new Headers({
                // "Accept": "application/json",
                // "Content-Type": "application/json"
                'Content-Type':'application/x-www-form-urlencoded;charset=UTF-8'
            })
        })

    }

    ObserverHttpPostForm(url, params) {
        return this.http.post(environment.domain + url, null, {      //app
            params: params,
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded,charset=UTF-8'
            })
        })
    }

    //post请求
    ObserverHttpPost(url, params) {
        return this.http.post(environment.domain + url, null, {      //app
            params: this.encode(params, 'post'),
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            })
        })
    }

    //post请求
    ObserverHttpNoForm(url, params) {
        return this.http.post(environment.domain + url, null, {      //app
            params: params,
            headers: new Headers({
                'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8'
            })
        })

    }

    //post请求 params+body
    ObserverHttpPostParamsBody(url, params, data) {
        return this.http.post(environment.domain + url + params, data, {      //app
            headers: new Headers({
                "Content-Type": "application/json"
            })
        })
    }

    //put请求
    ObserverHttpPut(url, params, data) {
        return this.http.put(environment.domain + url + params, data, {      //app
            headers: new Headers({
                "Content-Type": "application/json"
            })
        })
    }

}
