package com.adealink.weparty.effect

import android.graphics.Bitmap
import com.adealink.frame.base.AppBase
import com.adealink.frame.coroutine.dispatcher.Dispatcher
import com.adealink.frame.image.imageService
import com.adealink.frame.image.listener.IImageLoadResultListener
import com.adealink.frame.util.AppUtil
import com.adealink.weparty.debug.DebugPrefs
import com.opensource.svgaplayer.control.SvgaConfig
import com.opensource.svgaplayer.disk.DiskCacheParamsSupplier
import com.opensource.svgaplayer.disk.DiskTrimStrategy
import com.opensource.svgaplayer.executor.ExecutorsSupplier
import com.opensource.svgaplayer.remote.ImageFetchCallback
import com.opensource.svgaplayer.remote.ImageFetcher
import com.opensource.svgaplayer.utils.ByteConstants
import java.io.File
import java.util.concurrent.Executor
import java.util.concurrent.TimeUnit

val SVGA_CONFIG = SvgaConfig(
    diskCacheParamsSupplier = object : DiskCacheParamsSupplier {
        override fun getPath(): File = File(AppUtil.appContext.cacheDir, "svga")

        override fun getDiskTrimStrategy(): DiskTrimStrategy {
            return object : DiskTrimStrategy {
                override fun maxSize(): Long = ByteConstants.MB * 100L
                override fun timeExpired(): Long = TimeUnit.DAYS.toMillis(10)
            }
        }
    },
    executorsSupplier = object : ExecutorsSupplier {

        override fun getBackgroundExecutor(): Executor {
            return Dispatcher.wenextThreadPoolExecutor
        }

        override fun getIOExecutor(): Executor {
            return Dispatcher.wenextThreadPoolExecutor
        }

        override fun getNetWorkExecutor(): Executor {
            return Dispatcher.wenextThreadPoolExecutor
        }

    },
    debuggable = !AppBase.isRelease,
    imageFetcher = object : ImageFetcher {

        override fun fetchDynamicImage(
            url: String,
            forKey: String,
            callback: ImageFetchCallback?
        ) {
            imageService.fetchImage(url, object : IImageLoadResultListener {
                override fun onSuccess(bitmap: Bitmap) {
                    super.onSuccess(bitmap)
                    callback?.onFetched(bitmap, forKey)
                }

                override fun onFailed() {
                    super.onFailed()
                    callback?.onFailure(Exception("load image failed"), forKey)
                }

                override fun onCancel() {
                    super.onCancel()
                    callback?.onFailure(Exception("load image canceled"), forKey)
                }
            })
        }

    },
    designWidthPx = 750f,
    enableSvgaChecker = !AppBase.isRelease && DebugPrefs.enableSvgaChecker,
    enableShowSvgaCheckResult = !AppBase.isRelease && DebugPrefs.enableSvgaChecker && DebugPrefs.enableShowSvgaCheckResult
)