package com.zswc.creativity.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.core.net.toUri
import com.luck.picture.lib.PictureSelector
import com.zswc.creativity.base.MBActivity
import com.zswc.creativity.config.MIntentAction
import com.zswc.creativity.databinding.ActivityBankBinding
import com.zswc.creativity.inte.OnUploadCallback
import com.zswc.creativity.utils.FileUtils
import com.zswc.creativity.utils.OssUtils
import com.zswc.creativity.vmodel.BankVModel
import java.util.ArrayList

/**
 *Created by shihuiyun
 *on 2020/9/21
 */
class BankActivity : MBActivity<BankVModel, ActivityBankBinding>(), OnUploadCallback<String> {

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

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

    override fun vmClass(): Class<BankVModel> {
        return BankVModel::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)
                }

                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")
    }

}
