package com.example.tyre;

import android.app.Application;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.Toast;


import com.android.hdhe.uhf.reader.UhfReader;
import com.example.tyre.util.PlayMusic;
import com.example.tyre.util.SharedPreferencesUtils;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheEntity;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.cookie.CookieJarImpl;
import com.lzy.okgo.cookie.store.MemoryCookieStore;
import com.lzy.okgo.https.HttpsUtils;
import com.lzy.okgo.interceptor.HttpLoggingInterceptor;
//import com.seuic.uhf.UHFService;

import java.util.concurrent.TimeUnit;
import java.util.logging.Level;

import okhttp3.OkHttpClient;

//import com.port.Adapt;


/**
 * @author: wangh
 * @description:
 * @date: 2019/06/11-13:01
 */
public class MyApplication extends Application {
    private static UhfReader manager;
    private static MyApplication instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        SharedPreferencesUtils.init(getApplicationContext(), "myapp");
        okGoBase();
        manager = UhfReader.getInstance();
        if (manager != null) {
            manager.setOutputPower(26);
            manager.setWorkArea(0);
        } else {
            showToast("扫描服务初始化失败");
        }
    }

    public static UhfReader getManager() {
        if (manager == null) {
            showToast("扫描服务未启动");
            return null;
        }
        return manager;
    }

    public static MyApplication getInstance() {
        return instance;
    }

    private void okGoBase() {
        // 初始化网络请求框架
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor("OkGo");
        loggingInterceptor.setPrintLevel(HttpLoggingInterceptor.Level.BODY);        // log打印级别，决定了log显示的详细程度
        loggingInterceptor.setColorLevel(Level.SEVERE);                             // log颜色级别，决定了log在控制台显示的颜色
        HttpsUtils.SSLParams sslParams1 =
                HttpsUtils.getSslSocketFactory();         // 方法一：信任所有证书,不安全有风险
        builder.sslSocketFactory(sslParams1.sSLSocketFactory, sslParams1.trustManager);
        builder.addInterceptor(loggingInterceptor);
        builder.readTimeout(9000, TimeUnit.MILLISECONDS);           // 全局的读取超时时间
        builder.writeTimeout(9000, TimeUnit.MILLISECONDS);          // 全局的写入超时时间
        builder.connectTimeout(9000, TimeUnit.MILLISECONDS);        // 全局的连接超时时间
        builder.cookieJar(new CookieJarImpl(new MemoryCookieStore()));       // 使用内存保持cookie，app退出后，cookie消失
        OkGo.getInstance().init(this).setOkHttpClient(builder.build())  // 建议设置OkHttpClient，不设置将使用默认的
                .setCacheMode(CacheMode.NO_CACHE)                            // 全局统一缓存模式，默认不使用缓存，可以不传
                .setCacheTime(CacheEntity.CACHE_NEVER_EXPIRE)                // 全局统一缓存时间，默认永不过期，可以不传
                .setRetryCount(0);
    }

    // 添加Toast方法
    public static void showToast(String msg) {
        Toast.makeText(getInstance().getApplicationContext(), msg, Toast.LENGTH_SHORT).show();
    }

}
