package com.adealink.weparty.download

import com.adealink.frame.base.Rlt
import com.adealink.frame.base.WriterCallback
import com.adealink.frame.download.config.IDownloadConfig
import com.adealink.frame.download.constant.DownloadResultData
import com.adealink.frame.network.stat.NetMonitorStatEvent
import com.adealink.frame.oss.IOssService
import com.adealink.frame.storageService
import com.adealink.weparty.App
import okhttp3.OkHttpClient

class DownloadConfig : IDownloadConfig {

    override val ossBucket: String
        get() = ""
    override val ossService: IOssService
        get() = App.instance.ossService

    override val downloadHttpClient: OkHttpClient
        get() = App.instance.networkService.fileHttpClient

    override fun getCacheRootDir(): String {
        return storageService.file.getCacheRootDir()
    }

    override fun getTempUsingDir(): String {
        return storageService.file.getTempUsingDir()
    }

    override fun report(data: DownloadResultData) {
        val (url, host, size, duration, protocol, tlsVersion, awsXAmzCfId) = data
        NetMonitorStatEvent(NetMonitorStatEvent.Action.TRAFFIC)
            .apply {
                uri to url
                this.host to host
                amount to size
                this.duration to duration
                this.protocol to protocol
                this.tlsVersion to tlsVersion
                xAmzCfId to awsXAmzCfId
            }
            .send()
    }

    override suspend fun saveFile(savePath: String, callback: WriterCallback): Rlt<Any> {
        return storageService.file.saveFile(savePath, callback)
    }

}