package com.casic.br.view

import android.graphics.Color
import android.util.Log
import com.bumptech.glide.Glide
import com.bumptech.glide.request.RequestOptions
import com.casic.br.R
import com.casic.br.extensions.compressImage
import com.casic.br.utils.DeserializeModel
import com.casic.br.utils.GlideLoadEngine
import com.casic.br.utils.LocaleConstant
import com.casic.smarttube.callback.OnImageCompressListener
import com.gyf.immersionbar.ImmersionBar
import com.luck.picture.lib.basic.PictureSelector
import com.luck.picture.lib.config.SelectMimeType
import com.luck.picture.lib.entity.LocalMedia
import com.luck.picture.lib.interfaces.OnResultCallbackListener
import com.pengxh.kt.lite.base.KotlinBaseActivity
import com.pengxh.kt.lite.extensions.*
import com.pengxh.kt.lite.utils.ImmerseStatusBarUtil
import com.pengxh.kt.lite.widget.dialog.BottomActionSheet
import kotlinx.android.synthetic.main.activity_edit_user.*
import kotlinx.android.synthetic.main.activity_edit_user.userNameView
import kotlinx.android.synthetic.main.activity_edit_user.userPhoneView
import kotlinx.android.synthetic.main.fragment_mine.*
import kotlinx.android.synthetic.main.include_base_title.*
import java.io.File
import java.util.*


class EditUserActivity : KotlinBaseActivity() {

    private val kTag = "EditUserActivity"
    private val context = this@EditUserActivity

    override fun initLayoutView(): Int = R.layout.activity_edit_user

    override fun setupTopBarLayout() {
        ImmersionBar.with(this).statusBarDarkFont(true).init()
        ImmerseStatusBarUtil.setColor(this, R.color.white.convertColor(this))

        leftBackView.setOnClickListener { finish() }
        titleView.text = "个人中心"
    }

    override fun initData() {
        val userModel = DeserializeModel.getUserModel() ?: return
        userNameView.text = resources.getString(R.string.app_name) +
                userModel.username.subSequence(10, 14)
        userPhoneView.text = userModel.mobile
//        genderView.text = userModel.mobile
//        birthdayView.text = userModel.mobile
    }

    override fun observeRequestState() {

    }

    override fun initEvent() {
        avatarLayout.setOnClickListener {
            BottomActionSheet.Builder()
                .setContext(this)
                .setActionItemTitle(listOf("拍照", "相册"))
                .setItemTextColor(Color.BLUE)
                .setOnActionSheetListener(object : BottomActionSheet.OnActionSheetListener {
                    override fun onActionItemClick(position: Int) {
                        when (position) {
                            0 -> {
                                PictureSelector.create(context)
                                    .openCamera(SelectMimeType.ofImage())
                                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                                        override fun onResult(result: ArrayList<LocalMedia>?) {
                                            if (result == null) {
                                                "拍照保存失败，请重试".show(context)
                                                return
                                            }
                                            analyticalSelectResults(result[0])
                                        }

                                        override fun onCancel() {

                                        }
                                    })
                            }
                            1 -> {
                                PictureSelector.create(context)
                                    .openGallery(SelectMimeType.ofImage())
                                    .isGif(false)
                                    .isMaxSelectEnabledMask(true)
                                    .setFilterMinFileSize(100)
                                    .setMaxSelectNum(1)
                                    .isDisplayCamera(false)
                                    .setImageEngine(GlideLoadEngine.instance)
                                    .forResult(object : OnResultCallbackListener<LocalMedia> {
                                        override fun onResult(result: ArrayList<LocalMedia>?) {
                                            if (result == null) {
                                                "选择照片失败，请重试".show(context)
                                                return
                                            }
                                            analyticalSelectResults(result[0])
                                        }

                                        override fun onCancel() {

                                        }
                                    })
                            }
                        }
                    }
                }).build().show()
        }

        userNameLayout.setOnClickListener {

        }

        userPhoneLayout.setOnClickListener {

        }

        genderLayout.setOnClickListener {
            BottomActionSheet.Builder()
                .setContext(this)
                .setActionItemTitle(LocaleConstant.GENDER_ARRAY)
                .setItemTextColor(Color.BLUE)
                .setOnActionSheetListener(object : BottomActionSheet.OnActionSheetListener {
                    override fun onActionItemClick(position: Int) {

                    }
                })
                .build().show()
        }

        birthdayLayout.setOnClickListener {

        }
    }

    private fun analyticalSelectResults(result: LocalMedia) {
        //压缩图片后上传
        result.realPath.compressImage(this, object : OnImageCompressListener {
            override fun onSuccess(file: File) {
                Log.d(kTag, "onSuccess: " + file.absolutePath)
                //上传图片
            }

            override fun onError(e: Throwable) {
                e.printStackTrace()
            }
        })
    }
}