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 ReSexActivity : 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()
        t_title.text="修改性别"
        val extra_sex = intent.getStringExtra("extra_sex")
        val hint_text=extra_sex+" "+"（0代表男，1代表女）"
        et_item.setHint(hint_text)
        back2my.setOnClickListener() {
            finish()
        }
        val sexChoice = arrayOf("男", "女", "未知")
        btnOk.setOnClickListener {
            var flag: Boolean = false
            val user_account: String = et_account.text.toString()
            var curSex = et_item.text.toString()
            val intent = Intent(this, ReMyinfoActivity::class.java)
            if (user_account.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 //找到
                                    if (curSex.toInt() in sexChoice.indices) {
                                        if(curSex.toInt()==0)
                                            i.setsex("男")
                                        else if(curSex.toInt()==1)
                                            i.setsex("女")
                                        else
                                            i.setsex("未知")
                                        i.update(i.objectId, object : UpdateListener() {
                                            override fun done(p0: BmobException?) {
                                                if (p0 == null) {
                                                    Toast.makeText(
                                                        this@ReSexActivity,
                                                        "性别修改成功！",
                                                        Toast.LENGTH_SHORT
                                                    ).show()
                                                    //跳转个人信息页面，但是跳转之后原来的信息消失，需要重新进才能刷新
                                                    startActivity(intent)
//                                                startActivityForResult(intent,1)
                                                    finish()

                                                } else {
                                                    Log.d("myLog", "can't modify name")
                                                }
                                            }
                                        })
                                    } else {
                                        Toast.makeText(
                                            this@ReSexActivity,
                                            "请输入‘男’、‘女、’未知‘",
                                            Toast.LENGTH_SHORT
                                        ).show()
                                    }
                                }
                                else{
                                    Toast.makeText(
                                        this@ReSexActivity,
                                        "请输入自己的账号！",
                                        Toast.LENGTH_SHORT
                                    ).show()
                                }
                            }
                            if (flag != true)
                                Toast.makeText(
                                    this@ReSexActivity,
                                    "账号不存在，请重新输入！",
                                    Toast.LENGTH_SHORT
                                ).show()
                        }
                    }
                })
            }

        }
    }
}