package com.zswc.creativity.activity

import android.Manifest
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.net.toUri
import androidx.lifecycle.lifecycleScope
import com.eazypermissions.common.model.PermissionResult
import com.eazypermissions.coroutinespermission.PermissionManager
import com.luck.picture.lib.PictureSelector
import com.qmuiteam.qmui.kotlin.onClick
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.config.MIntentAction
import com.zswc.creativity.databinding.ActivityMyInfoBinding
import com.zswc.creativity.databinding.ActivitySpreadBinding
import com.zswc.creativity.inte.OnUploadCallback
import com.zswc.creativity.utils.ChosePhotoUtils
import com.zswc.creativity.utils.FileUtils
import com.zswc.creativity.utils.OssUtils
import com.zswc.creativity.vmodel.MyInfoVModel
import com.zswc.creativity.vmodel.SpreadVModel
import kotlinx.coroutines.launch
import okhttp3.MediaType
import okhttp3.MultipartBody
import okhttp3.RequestBody
import java.io.File
import java.util.ArrayList

/**
 * 我的账户信息
 *Created by shihuiyun
 */
class MyInfoActivity : MBActivity<MyInfoVModel, ActivityMyInfoBinding>(), OnUploadCallback<String> {

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)

    }

    override fun listeners() {
        super.listeners()
        binding.imgAvator.onClick {
            lifecycleScope.launch {
                if (PermissionManager.requestPermissions(
                                this@MyInfoActivity, 1000,
                                Manifest.permission.CAMERA,
                                Manifest.permission.WRITE_EXTERNAL_STORAGE,
                                Manifest.permission.READ_EXTERNAL_STORAGE
                        ) is PermissionResult.PermissionGranted
                ) {

                    ChosePhotoUtils.getChosePhoto(view()!!.context(), 1)
                }

            }
        }
    }

    override fun binding(): ActivityMyInfoBinding {
       return ActivityMyInfoBinding.inflate(layoutInflater)
    }

    override fun vmClass(): Class<MyInfoVModel> {
        return MyInfoVModel::class.java
    }

    override fun title(): String? {
        return "账户信息"
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode != Activity.RESULT_OK) {
            return
        }
        if (requestCode == MIntentAction.REQUEST_CODE_THREE) {//图片

            val selectList = PictureSelector.obtainMultipleResult(data)
            val list = ArrayList<String>()
            if (null != selectList) {
                if(selectList[0].path.contains("content://")){

                    val cr = this.contentResolver

                    list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

                }else{
                    list.add(selectList[0].path)
                }

                vm.toUpImg(list[0])
//                OssUtils(this,vm)
//                        .setUpLoad("bank", list, this)

            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            if(selectList[0].path.contains("content://")){

                val cr = this.contentResolver
                list.add( FileUtils.getFilePathFromContentUri(selectList[0].path.toUri(),cr))

            }else{
                list.add(selectList[0].path)
            }
            OssUtils(this, vm)
                    .setUpLoad("bank", list, this)

            return
        }

    }

    override fun onUploadSuccess(t: String) {
        val ss: Array<String> = t.split(",".toRegex()).toTypedArray()
        //ss[0]--//不完整路径-接口需要   ss[1]完整路径

        Log.e("-shy-", "ss[1]=: "+ss[1] )

//        vm.image = ss[1]
//        vm.discernBank(ss[1])


    }

    override fun onUploadError(error: String?) {
        TODO("Not yet implemented")
    }


}
