package com.quansu.heifengwuliu.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import com.luck.picture.lib.PictureSelector
import com.quansu.heifengwuliu.R
import com.quansu.heifengwuliu.base.MBActivity
import com.quansu.heifengwuliu.config.MIntentAction
import com.quansu.heifengwuliu.databinding.ActivityPersonalVerifyBinding
import com.quansu.heifengwuliu.inte.OnUploadCallback
import com.quansu.heifengwuliu.repository.PersonalVerifyRepository
import com.quansu.heifengwuliu.utils.OssUtils
import com.quansu.heifengwuliu.vmodel.PersonalVerityVModel
import com.ysnows.base.base.BRepository
import java.util.*

class PersonalVerityActivity : MBActivity<PersonalVerityVModel, ActivityPersonalVerifyBinding>(), OnUploadCallback<String> {


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

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

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

    override fun createRepository(): BRepository {
        return PersonalVerifyRepository()
    }


    override fun title(): String? {
        return getString(R.string.identity_verify)
    }

    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) {
                for (path in selectList) {
                    list.add(path.path)
                }

                OssUtils(this)
                        .setUpLoad("idcard", list, this)

            }

            return

        }
        if (requestCode == MIntentAction.REQUEST_CODE_CAMERA) {//拍照
            //拍照
            val selectList = PictureSelector.obtainMultipleResult(data) ?: return
            val list = ArrayList<String>()
            list.add(selectList[0].path)
            OssUtils(this)
                    .setUpLoad("idcard", list, this)

            return
        }

    }

    override fun onUploadSuccess(t: String) {

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

        if (vm.type == "1") {
            vm.imgIDMan.value = ss[1]
        } else {
            vm.imgIDEmblem.value = ss[1]
        }


    }

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