package com.ydl.ydlcommon.app

import android.annotation.SuppressLint
import android.app.Application
import com.alibaba.android.arouter.launcher.ARouter
import com.lzy.imagepicker.ImagePicker
import com.lzy.imagepicker.view.CropImageView
import com.ydl.devicesidlib.DeviceIDHelper
import com.ydl.ydlcommon.actions.imagepicker.YdlImageLoader
import com.ydl.ydlcommon.utils.BuryPointUtils
import com.ydl.ydlcommon.utils.log.LogHelper
import com.yidianling.common.tools.RxTool


/**
 * 用于获取全局 context 和初始化一些第三方库
 * author : Zhangwenchao
 * e-mail : zhangwch@yidianling.com
 * time   : 2018/01/27
 */
@SuppressLint("StaticFieldLeak")
object YdlRepository {

    // 需要在主 module 的 application 的 onCreate 方法中调用此方法
    fun initApp(app: Application,isDebug:Boolean) {
        DeviceIDHelper.getInstance().init(app.applicationContext)
        LogHelper.getInstance().deleteExpireLogSync()
        //初始化工具类
        RxTool.init(app)
        BuryPointUtils.init()
        initImagePicker()

//        if (LeakCanary.isInAnalyzerProcess(app)) {
//            // This process is dedicated to LeakCanary for heap analysis.
//            // You should not init your app in this process.
//            return
//        }
//        refWatcher = LeakCanary.install(app)
        initARouter(app, isDebug)
    }

    /**
     * 初始化阿里路由
     */
    private fun initARouter(app: Application,isDebug:Boolean) {
        if (isDebug) {
            ARouter.openLog()
            ARouter.openDebug()
        }
        ARouter.init(app)
    }

    private fun initImagePicker() {
        val imagePicker = ImagePicker.getInstance()
        imagePicker.imageLoader = YdlImageLoader()   //设置图片加载器
        imagePicker.isEdit = true   //设置图片是否可编辑
        imagePicker.isShowCamera = true  //显示拍照按钮
        imagePicker.isCrop = true        //允许裁剪（单选才有效）
        imagePicker.isSaveRectangle = true //是否按矩形区域保存
        imagePicker.selectLimit = 9    //选中数量限制
        imagePicker.style = CropImageView.Style.RECTANGLE  //裁剪框的形状
        imagePicker.focusWidth = 800   //裁剪框的宽度。单位像素（圆形自动取宽高最小值）
        imagePicker.focusHeight = 800  //裁剪框的高度。单位像素（圆形自动取宽高最小值）
        imagePicker.outPutX = 1000//保存文件的宽度。单位像素
        imagePicker.outPutY = 1000//保存文件的高度。单位像素
    }

}