package com.bc_chat.bc_base.entity

import android.content.Context
import android.net.Uri
import android.text.TextUtils
import android.util.Base64
import com.zhaohaoting.framework.utils.DataCache
import org.json.JSONObject
import java.io.BufferedOutputStream
import java.io.File
import java.io.FileOutputStream
import java.io.IOException
import java.lang.Exception

/**
 * author：Created by zhaohaoting on 2019/3/16
 * email：526309416@qq.com
 * desc：
 */
class Collect(val id: Int? = 0) {
    var type: Int? = 1 //1-文字 2-图片 3-语音 4-视频
    var content: String? = ""
    var c_time: String? = ""

    //本地文件路径
    var voiceUri: Uri? = null

    var thumbUri: Uri? = null


    fun parseVoice(context: Context): Uri? {
        if (voiceUri == null) {
            try {
                val json = JSONObject(content)
                val base64 = json.optString("content")
                val fileName = id?.hashCode().toString() + System.currentTimeMillis() + ".amr"
                var file = File(DataCache.getVoiceCacheDir(context) + fileName)
                if (!TextUtils.isEmpty(base64) && !file.exists()) {
                    try {
                        val data = Base64.decode(base64, Base64.NO_WRAP)
                        file = saveFile(data, DataCache.getVoiceCacheDir(context), fileName)
                    } catch (var7: IllegalArgumentException) {
                        var7.printStackTrace()
                    } catch (var8: IOException) {
                        var8.printStackTrace()
                    }
                }
                voiceUri = Uri.fromFile(file)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return voiceUri
    }

    @Throws(IOException::class)
    private fun saveFile(data: ByteArray, path: String, fileName: String): File {
        var file = File(path)
        if (!file.exists()) {
            file.mkdirs()
        }

        file = File(path + fileName)
        val bos = BufferedOutputStream(FileOutputStream(file))
        bos.write(data)
        bos.flush()
        bos.close()
        return file
    }

    fun parseSight(context: Context): Uri? {
        if (thumbUri == null) {
            try {
                val json = JSONObject(content)
                val base64 = json.optString("content")
                val fileName = id?.hashCode().toString() + System.currentTimeMillis() + ".jpg"
                var file = File(DataCache.getPicCacheDir(context) + fileName)
                if (!TextUtils.isEmpty(base64) && !file.exists()) {
                    try {
                        val data = Base64.decode(base64, Base64.NO_WRAP)
                        file = saveFile(data, DataCache.getPicCacheDir(context), fileName)
                    } catch (var7: IllegalArgumentException) {
                        var7.printStackTrace()
                    } catch (var8: IOException) {
                        var8.printStackTrace()
                    }
                }
                thumbUri = Uri.fromFile(file)
            } catch (e: Exception) {
                e.printStackTrace()
            }
        }
        return thumbUri
    }
}