package edu.whut.jianshu.Activity

import android.content.Context
import android.content.Intent
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.bumptech.glide.Glide
import edu.whut.jianshu.Data.entity.User_bmob
import edu.whut.jianshu.NavigationHelper
import edu.whut.jianshu.R
import kotlinx.android.synthetic.main.activity_book_info.*
import kotlinx.android.synthetic.main.activity_my.*
import kotlinx.android.synthetic.main.layout_bottom_navigation.*
import java.io.InputStream
import java.net.HttpURLConnection
import java.net.URL

class MyActivity : AppCompatActivity() {
    lateinit var userId:String
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)
        userId = getSharedPreferences("userinf", Context.MODE_PRIVATE).getString("user_id", "").toString()
        val query = BmobQuery<User_bmob>()
        query.addWhereEqualTo("objectId", userId)
        query.findObjects(object : FindListener<User_bmob>() {
            override fun done(p0: MutableList<User_bmob>?, p1: BmobException?) {
                if (p1 == null) {
                   if(p0!=null&&p0.size>0) {
                       for (p in p0) {
                           tUserName.text=p.nickName
                           tUserSignature.text=p.signature
                           Glide.with(this@MyActivity) // 传入 Context
                               .load(p.face.url) // 加载图片的 URL
                               .placeholder(R.drawable.pre_load) // 设置占位图
                               .error(R.drawable.fail_load) // 设置加载失败时显示的图片
                               .into(iv_avatar) // 将图片加载到指定的 ImageView 中
                       }
                   }
                }
            }
        })
        //个人信息修改
        re_myinfo.setOnClickListener {
            val intent= Intent(this,ReMyinfoActivity::class.java)
            startActivity(intent)
        }
        //我的发布
        myPost.setOnClickListener {
            val intent= Intent(this, edu.whut.jianshu.Activity.MyPostActivity::class.java)
            startActivity(intent)
        }
        //账号管理
        account_manager.setOnClickListener{
            val intent= Intent(this, edu.whut.jianshu.Activity.AccountManagerActivity::class.java)
            startActivity(intent)
        }

        tuichu.setOnClickListener {
            val intent= Intent(this,LoginActivity::class.java)
            startActivity(intent)
            finish()
        }
        setupBottomNavigation()
    }
    private fun setupBottomNavigation() {
        homepage.setImageResource(R.drawable.my2)
        bookcity.setOnClickListener {
            NavigationHelper.navigateTo(this, MainActivity::class.java)
        }
        bookshelf.setOnClickListener {
            NavigationHelper.navigateTo(this, BookShelfActivity::class.java)
        }
        reading_comprehension.setOnClickListener {
            NavigationHelper.navigateTo(this, PerceptionActivity::class.java)
        }
    }
}

