package com.zjgsu.jianshu

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.UpdateListener
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_re_myinfo.*
import kotlinx.android.synthetic.main.activity_re_name.*

class ReSignatureActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") //连接bmob

        setContentView(R.layout.activity_re_name)
        t_title.setText("修改个性签名")

        val extra_account = intent.getStringExtra("extra_account")
        val extra_signature=intent.getStringExtra("extra_signature")
        et_rename.setHint(extra_signature)

        back2my.setOnClickListener(){
            val intent = Intent(this, ReMyinfoActivity::class.java)
            startActivity(intent)
            finish()
        }
        btnOk.setOnClickListener {
            var flag: Boolean = false
            val user_account: String = et_account.text.toString()
            var curSignature=et_rename.text.toString()
            //
//            val intent=Intent(this,ReMyinfoActivity::class.java)
            val intent= Intent(this,ReMyinfoActivity::class.java)
            if (user_account.isEmpty() ) {
                Toast.makeText(this, "账号不能为空！", Toast.LENGTH_SHORT).show()
                Log.d("zy", user_account)
            } else {
                val bmobQuery = BmobQuery<User_bmob>()
                var tempPwd: String = ""
                bmobQuery.findObjects(object : FindListener<User_bmob>() {
                    override fun done(list: List<User_bmob>, e: BmobException?) {
                        if (e == null) {
                            for (i in list) {
                                if (user_account.equals(i.account)) {
                                    flag = true //找到
                                    Log.d("myLog", "find sucessflly")
                                    //-------更新数据库中的nickName
                                    i.setsignature(curSignature)
                                    i.update(i.objectId, object : UpdateListener() {
                                        override fun done(p0: BmobException?) {
                                            if (p0 == null) {
                                                Toast.makeText(
                                                    this@ReSignatureActivity,
                                                    "个性签名修改成功！",
                                                    Toast.LENGTH_SHORT
                                                ).show()
                                                //跳转个人信息页面，但是跳转之后原来的信息消失，需要重新进才能刷新
                                                startActivity(intent)
//                                                startActivityForResult(intent,1)
                                                finish()
                                            } else {
                                                Log.d("myLog", "can't modify name")
                                            }
                                        }
                                    })

                                }
                            }
                        }
                        else {
                            Log.d("myLog","error")
                        }
                        if (flag != true)
                            Toast.makeText(
                                this@ReSignatureActivity,
                                "账号不存在，请重新输入！",
                                Toast.LENGTH_SHORT
                            ).show()
                    }
                })
            }
        }


    }
}