package com.wd.workoffice.app;

import android.app.Application;

import com.scwang.smartrefresh.header.BezierCircleHeader;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.tencent.bugly.crashreport.CrashReport;
import com.wd.workoffice.R;
import com.wd.workoffice.retrofit.ApiService;
import com.wd.workoffice.retrofit.MesApiService;
import com.wd.workoffice.retrofit.RtfMesHelper;
import com.wd.workoffice.util.UserKeeper;

import cn.jpush.android.api.JPushInterface;
import flexible.xd.android_base.base.BaseApp;
import flexible.xd.android_base.network.rtfhttp.RtfHelper;
import flexible.xd.android_base.utils.Utils;
import me.jessyan.autosize.AutoSizeConfig;
import me.jessyan.autosize.unit.Subunits;
import okhttp3.Request;


/**
 * Created by flexibleXd on 2016/12/22.
 */

public class WorkApp extends BaseApp {
    private static Application instance;

    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        CrashReport.initCrashReport(getApplicationContext(), "8081eafad1", false);
        Utils.init(this);
        AutoSizeConfig.getInstance().setCustomFragment(true);
        AutoSizeConfig.getInstance().getUnitsManager()
                .setSupportDP(false)
                .setSupportSP(false)
                .setSupportSubunits(Subunits.MM);
        RtfHelper.getInstance().init(ApiService.DOMAIN, chain -> {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("Authorization", "Bearer " + UserKeeper.getInstance().getToken())
                    .header("userId", UserKeeper.getInstance().getUserId())
                    .method(original.method(), original.body())
                    .build();

            return chain.proceed(request);
        });
        RtfMesHelper.getInstance().init(MesApiService.DOMAIN, chain -> {
            Request original = chain.request();
            Request request = original.newBuilder()
                    .header("Cookie", "SESSION=0514c62e-9e13-4382-be68-b499ec39b3df")
                    .method(original.method(), original.body())
                    .build();

            return chain.proceed(request);
        });
        smartInit();
        JPushInterface.setDebugMode(true);
        JPushInterface.init(this);

    }
//    private void daoInit() {
//        daoSession = DaoMaster.newDevSession(this, "workO.db");
//    }
//
//    public static DaoSession getDaoSession() {
//        return daoSession;
//    }

    private void smartInit() {
        SmartRefreshLayout.setDefaultRefreshInitializer((context, layout) -> {
            //开始设置全局的基本参数（可以被下面的DefaultRefreshHeaderCreator覆盖）
            layout.setReboundDuration(1000);
//                layout.setReboundInterpolator(new DropBounceInterpolator());
            layout.setFooterHeight(100);
            layout.setDisableContentWhenLoading(false);
            layout.setPrimaryColorsId(R.color.mainTextColor, android.R.color.white);
        });

        //全局设置默认的 Header
        SmartRefreshLayout.setDefaultRefreshHeaderCreator((context, layout) -> {
            //开始设置全局的基本参数（这里设置的属性只跟下面的MaterialHeader绑定，其他Header不会生效，能覆盖DefaultRefreshInitializer的属性和Xml设置的属性）
            return new BezierCircleHeader(context);

        });
    }

//    @Override
//    protected void attachBaseContext(Context base) {
//        super.attachBaseContext(base);
//        MultiDex.install(base);
//    }

    public static synchronized Application getApp() {
        return instance;
    }
}
