package com.zjgsu.jianshu

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 android.util.Log
import cn.bmob.v3.Bmob
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import com.zjgsu.jianshu.Activity.AccountManagerActivity
import com.zjgsu.jianshu.Activity.MyPostActivity
import com.zjgsu.jianshu.Bmob.User_bmob
import kotlinx.android.synthetic.main.activity_main.*
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() {

    //登录的用户名
    public val user_account="0509"
    lateinit var userId:String

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_my)
        Bmob.initialize(this, "8f9f1d1ea520b0ce4f84a6fa83a5f754") //连接bmob
        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
                           object : Thread() {
                               override fun run() {
                                   try {
                                       val url = URL(p!!.face.url)
                                       val connection: HttpURLConnection =
                                           url.openConnection() as HttpURLConnection
                                       connection.setRequestMethod("GET")
                                       connection.setConnectTimeout(5000)
                                       val `in`: InputStream = connection.getInputStream()
                                       val bitmap: Bitmap =
                                           BitmapFactory.decodeStream(`in`)
                                       iv_avatar.setImageBitmap(bitmap)
                                   } catch (e: Exception) {
                                       e.printStackTrace()
                                   }
                               }
                           }.start()
                       }
                   }
                }
            }
        })
        //个人信息修改
        re_myinfo.setOnClickListener {
            val extra_account=user_account
            val intent= Intent(this,ReMyinfoActivity::class.java)
            intent.putExtra("extra_account",extra_account)
//            Log.d("helloSend","$signatureData")
//            Log.d("helloSend","$userData")
            startActivity(intent)
            finish()
        }
        //我的发布
        myPost.setOnClickListener {
            val intent= Intent(this,MyPostActivity::class.java)
            startActivity(intent)
            finish()
        }
        //账号管理
        account_manager.setOnClickListener{
            val intent= Intent(this,AccountManagerActivity::class.java)
            val userData=user_account
            intent.putExtra("extra_user_data",userData)
            startActivity(intent)
            finish()
        }

        tuichu.setOnClickListener {
            val intent= Intent(this,LoginActivity::class.java)
            userId=""
            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)
        }
        homepage.setOnClickListener {
            NavigationHelper.navigateTo(this, MyActivity::class.java)
        }
    }
}

