package com.zswc.creativity.activity

import android.app.Activity
import android.content.Intent
import android.os.Bundle
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.ActivityPasswordBinding
import com.zswc.creativity.inte.OnUploadCallback
import com.zswc.creativity.utils.FileUtils
import com.zswc.creativity.utils.OssUtils
import com.zswc.creativity.view.PasswordView.PasswordListener
import com.zswc.creativity.vmodel.PasswordVModel
import java.util.*

class PasswordActivity : MBActivity<PasswordVModel, ActivityPasswordBinding>(), OnUploadCallback<String> {


    var type=1//1：登录密码 2支付密码 3编辑资料
//    var oldPassword=""
    var newPassword=""

    override fun __before() {
        super.__before()

        type= intent?.extras?.getInt("type", 1)!!


    }

    override fun init(savedInstanceState: Bundle?) {
        super.init(savedInstanceState)
        vm.type.value= type
        if(type==2){
            //初始化密码输入框
            initPassword()
        }
        if(type==3){
            vm.setInitData()
        }
    }



   private fun initPassword(){


//       binding.ppetOldPassword.setPasswordListener(object :PasswordListener{
//           override fun keyEnterPress(password: String?, isComplete: Boolean) {
//
//               oldPassword= password!!
//               Log.e("-shy-", "oldPassword=: "+oldPassword )
//           }
//
//           override fun passwordChange(changeText: String?) {
//           }
//
//           override fun passwordComplete(password: String?) {
//               oldPassword=password!!
//           }
//
//       })

       binding.ppetNewPassword.setPasswordListener(object :PasswordListener{
           override fun keyEnterPress(password: String?, isComplete: Boolean) {

               newPassword= password!!
           }

           override fun passwordChange(changeText: String?) {

           }

           override fun passwordComplete(password: String?) {
               newPassword=password!!
           }

       })

       binding.flPaySubmit.setOnClickListener {
           vm.toPaySubmit(newPassword)
       }


   }




    override fun title(): String? {

        return when(type){
            1-> "设置登录密码"
            2-> "设置支付密码"
            3-> "编辑资料"
            else-> ""
        }

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

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

    override fun onUploadSuccess(t: String) {
        val ss: Array<String> = t.split(",".toRegex()).toTypedArray()
        vm.avatar.value = ss[1]
    }

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

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

                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("avatar", 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("avatar", list, this)

            return
        }

    }



}
