package edu.whut.jianshu.Activity

import android.content.Context
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.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.UpdateListener
import edu.whut.jianshu.Data.entity.User_bmob
import edu.whut.jianshu.R
import kotlinx.android.synthetic.main.activity_editinfo.*

class ReNameActivity : AppCompatActivity() {
    lateinit var userId:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_editinfo)
        userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
        back2my.setOnClickListener() {
            finish()
        }
        val oldName = intent.getStringExtra("extra_user_data")
        et_item.setHint(oldName)
        //读取输入
        btnOk.setOnClickListener {
            var flag: Boolean = false
            val user_account: String = et_account.text.toString()
            var curName: String = et_item.text.toString()

            val intent=Intent(this,MyActivity::class.java)
            if (user_account.isEmpty() || curName.isEmpty()) {
                Toast.makeText(this, "账号或昵称不能为空！", Toast.LENGTH_SHORT).show()
            } else {
                val bmobQuery = BmobQuery<User_bmob>()
                bmobQuery.addWhereEqualTo("objectId", userId)
                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.setnickName(curName)
                                    i.update(i.objectId, object : UpdateListener() {
                                        override fun done(p0: BmobException?) {
                                            if (p0 == null) {
                                                Toast.makeText(
                                                    this@ReNameActivity,
                                                    "昵称修改成功！",
                                                    Toast.LENGTH_SHORT
                                                ).show()
                                                startActivity(intent)
//                                                startActivityForResult(intent,1)
                                                finish()
                                            } else {
                                                Log.d("myLog", "can't modify name")
                                            }
                                        }
                                    })
                                }
                                else{
                                    Toast.makeText(
                                        this@ReNameActivity,
                                        "请输入自己的账号！",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                        }
                        else {
                            Log.d("myLog","error")
                        }
                        if (flag != true)
                            Toast.makeText(
                                this@ReNameActivity,
                                "账号不存在，请重新输入！",
                                Toast.LENGTH_SHORT
                            ).show()
                    }
                })
            }
        }
    }
}
