/*
 * ********************************************************************************************************************
 *
 *               iFHS7.
 *              ;BBMBMBMc                  rZMBMBR              BMB
 *              MBEr:;PBM,               7MBMMEOBB:             BBB                       RBW
 *     XK:      BO     SB.     :SZ       MBM.       c;;     ir  BBM :FFr       :SSF:    ;xBMB:r   iuGXv.    i:. iF2;
 *     DBBM0r.  :D     S7   ;XMBMB       GMBMu.     MBM:   BMB  MBMBBBMBMS   WMBMBMBBK  MBMBMBM  BMBRBMBW  .MBMBMBMBB
 *      :JMRMMD  ..    ,  1MMRM1;         ;MBMBBR:   MBM  ;MB:  BMB:   MBM. RMBr   sBMH   BM0         UMB,  BMB.  KMBv
 *     ;.   XOW  B1; :uM: 1RE,   i           .2BMBs  rMB. MBO   MBO    JMB; MBB     MBM   BBS    7MBMBOBM:  MBW   :BMc
 *     OBRJ.SEE  MRDOWOR, 3DE:7OBM       .     ;BMB   RMR7BM    BMB    MBB. BMB    ,BMR  .BBZ   MMB   rMB,  BMM   rMB7
 *     :FBRO0D0  RKXSXPR. JOKOOMPi       BMBSSWBMB;    BMBB:    MBMB0ZMBMS  .BMBOXRBMB    MBMDE RBM2;SMBM;  MBB   xBM2
 *         iZGE  O0SHSPO. uGZ7.          sBMBMBDL      :BMO     OZu:BMBK,     rRBMB0;     ,EBMB  xBMBr:ER.  RDU   :OO;
 *     ,BZ, 1D0  RPSFHXR. xWZ .SMr                  . .BBB
 *      :0BMRDG  RESSSKR. 2WOMBW;                   BMBMR
 *         i0BM: SWKHKGO  MBDv
 *           .UB  OOGDM. MK,                                          Copyright (c) 2015-2019.  斯伯坦机器人
 *              ,  XMW  ..
 *                  r                                                                     All rights reserved.
 *
 * ********************************************************************************************************************
 */

package com.sybotan.android.demo

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.SharedPreferences
import android.util.Log
import androidx.core.content.edit
import com.arashivision.sdkcamera.InstaCameraSDK
import com.arashivision.sdkmedia.InstaMediaSDK
import com.framework.app.DEVICE_ID
import com.framework.di.*
import com.sybotan.android.demo.tools.AssetsUtil
import org.kodein.di.DI
import org.kodein.di.DIAware
import org.kodein.di.android.x.androidXModule
import org.kodein.di.instance
import java.io.File
import java.util.*


class DemoApp : Application(), DIAware {

    override val di: DI by DI.lazy {
        // import(androidCoreModule(this@DemoApp))
        import(androidXModule(this@DemoApp))
        import(databaseModule)
        import(httpModule)
        import(repositoryModule)
        import(viewModelModule)
        import(androidModule)
        //import(factoryModule)
    }

    val sp by instance<SharedPreferences>()

    companion object {
        private val TAG = DemoApp::class.java.name
        var sInstance: DemoApp? = null
        var mContext: Context? = null

        fun getContext(): DemoApp? {
            return mContext as DemoApp
        }

        fun getInstance(): DemoApp? {
            return sInstance
        }

        val COPY_DIR: String = getInstance()?.cacheDir.toString() + "/hdr_source"
    }

    /**
     * 创建应用时调用
     */
    @SuppressLint("MissingSuperCall")
    override fun onCreate() {
        super.onCreate()
        sInstance = this
        Log.d(TAG, "DemoApp starting...")
        mContext = applicationContext

        // Init SDK
        InstaCameraSDK.init(this)
        InstaMediaSDK.init(this)

        // Copy sample pictures from assets to local
        copyHdrSourceFromAssets()
        val deviceId = sp.getString(DEVICE_ID, "") ?: ""

        if (deviceId.isEmpty()) {
            sp.edit {
                putString(DEVICE_ID, "${UUID.randomUUID()}")
            }
        }

        return
    } // Function onCreate()

    private fun copyHdrSourceFromAssets() {
        val dir: File = File(COPY_DIR)
        if (!dir.exists()) {
            AssetsUtil.copyFilesFromAssets(this, "hdr_source", dir.absolutePath)
        }
    }
} // Class DemoApp