package com.zjgsu.jianshu

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
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.SaveListener
import com.zjgsu.jianshu.Bmob.Book_info_bmob
import com.zjgsu.jianshu.Bmob.Perception_bmob
import com.zjgsu.jianshu.Book_Shelf
import com.zjgsu.jianshu.R
import kotlinx.android.synthetic.main.activity_sendspecific.*

class SendSpecificActivity: AppCompatActivity() {
    lateinit var userId:String
    lateinit var bookname:String
    var comment_rank: Int? = null
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sendspecific)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754")//连接bmob
        sendspecific_content.requestFocus()
        // 弹出软键盘
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.showSoftInput(sendspecific_content, InputMethodManager.SHOW_IMPLICIT)
        // 防止自动弹出软键盘时页面被顶起
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE)
        comment_rank = intent.getIntExtra("comment_rank",0)
        bookname = intent.getStringExtra("bookname")?: ""
        userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
        sendspecific_title.text="点评《"+bookname+"》"
        judgeComment()
        setClickListeners()
    }
    private fun judgeComment(){
        if(comment_rank==1)
            sendspecific_henhao.setImageResource(R.drawable.recommend2)
        else if(comment_rank==2)
            sendspecific_yiban.setImageResource(R.drawable.normal2)
        else if(comment_rank==3)
            sendspecific_buxing.setImageResource(R.drawable.bad2)
    }
    private fun setClickListeners(){
        sendspecific_cancel.setOnClickListener {
            val intent = Intent(this, BookInformationActivity::class.java)
            intent.putExtra("Book_name", bookname)
            startActivity(intent)
            finish()
        }
        sendspecific_submit.setOnClickListener {
            val perception=Perception_bmob()
            perception.b_name=bookname
            perception.userid=userId
            perception.perception=sendspecific_content.text.toString()
            perception.evaluation= comment_rank!!
            perception.save(object : SaveListener<String>() {
                override fun done(objectId: String?, e: BmobException?) {
                    if (e == null) {
                        // 保存成功
                        Toast.makeText(
                            this@SendSpecificActivity,
                            "点评成功！",
                            Toast.LENGTH_SHORT
                        ).show()
                        Log.d("sendspecific", "Perception_bmob saved successfully. objectId: $objectId")
                        val intent = Intent(this@SendSpecificActivity, BookInformationActivity::class.java)
                        intent.putExtra("Book_name", bookname)
                        startActivity(intent)
                    } else {
                        // 保存失败
                        Log.e("sendspecific", "Failed to save Perception_bmob: ${e.message}")
                    }
                }
            })
        }
    }
}