package com.zxhl.main.page.activity

import android.graphics.Color
import android.graphics.drawable.BitmapDrawable
import android.graphics.drawable.ColorDrawable
import android.net.Uri
import android.view.*
import android.widget.PopupWindow
import android.util.Log
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import com.bumptech.glide.Glide
import com.google.android.exoplayer2.ExoPlayer
import com.google.android.exoplayer2.MediaItem
import com.zxhl.cms.AppContext
import com.zxhl.cms.common.base.BaseActivity
import com.zxhl.cms.net.model.box.HomeListEntity
import com.zxhl.cms.pay.view.VipPayDialog
import com.zxhl.cms.utils.OnRecycleItemClickListener
import com.zxhl.cms.utils.ReporContentListener
import com.zxhl.cms.utils.Utils
import com.zxhl.main.R
import com.zxhl.main.page.`interface`.ReportListener
import com.zxhl.main.page.adapter.UserPhotosAdapter
import com.zxhl.main.page.contract.UserDetailsContract
import com.zxhl.main.page.presenter.UserDetailsPresenter
import com.zxhl.main.page.view.DialogUtils
import kotlinx.android.synthetic.main.activity_layout_user_details_hw.*


/**
 * @author (wangXuewei)
 * @datetime 2022-04-15 10:48 GMT+8
 * @detail :海外新版用户详情
 */
class UserDetailsActivity : BaseActivity(), UserDetailsContract.View,
    OnRecycleItemClickListener<String> {

    private var userId: String? = ""
    private var detailfree: String? = ""

    private var mPhotoAdapter: UserPhotosAdapter? = null;
    private var mPresenter: UserDetailsPresenter? = null

    override fun before() {
        super.before()
        setStatusBarBackground(AppContext.get().resources.getColor(R.color.transparent))
    }

    override fun init() {

        userId = intent?.data?.getQueryParameter("userId") ?: ""
        detailfree = intent?.data?.getQueryParameter("detailfree") ?: ""

        mPhotoAdapter = UserPhotosAdapter(this, this)
        mPresenter = UserDetailsPresenter(this)

        id_rcl_user_details_photo_list.layoutManager =
            LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
        id_rcl_user_details_photo_list.adapter = mPhotoAdapter

        id_img_btn_back.setOnClickListener(this)
        id_img_btn_report.setOnClickListener(this)
        id_img_user_details_sms.setOnClickListener(this)
        id_ll_user_details_call_video.setOnClickListener(this)
        id_ll_user_details_like_btn.setOnClickListener(this)

        mPresenter?.getUserDetails(userId, detailfree)
    }

    override fun onClick(v: View?) {
        when (v) {
            id_img_btn_back -> {
                finish()
            }
            id_img_btn_report -> {
                DialogUtils.showReport(this, object : ReportListener {
                    override fun report(pos: Int) {
                        if (pos == 0) {
                            DialogUtils.showBlackWarning(this@UserDetailsActivity,View.OnClickListener {
                                mPresenter?.blackUser(userId)
                            })

                        }  else if (pos == 2) {
                            DialogUtils.showReportContent(this@UserDetailsActivity,object :ReporContentListener{
                                override fun toReport(string: String) {
                                  mPresenter?.reportUser(userId,string)
                                }

                            })

                        }
                        // showToast("Pos"+pos)
                    }

                })

            }
            id_img_user_details_sms -> {
                showToast("sms")
            }
            id_ll_user_details_call_video -> {
                VipPayDialog.showVipPayDialog(this)
            }
            id_ll_user_details_like_btn -> {
                isLike = !isLike
                mPresenter?.likeUser(userId, detailfree)
            }
        }
    }


    override fun layoutID(): Int {
        return R.layout.activity_layout_user_details_hw
    }

    private var userDetails: HomeListEntity? = null;
    override fun setUserDetails(result: HomeListEntity) {
        userDetails = result;
        //Name
        id_tv_user_details_name.text = result.nickname
        //国旗
        Glide.with(this).load(result.nationalFlag ?: "").into(id_img_national_flag)
        //年龄
        id_tv_user_details_age.text = result.age
        //是否在线
        if (result.onlineStatus.equals("0")) {
            id_tv_user_details_online.text = "Offline"
        } else {
            id_tv_user_details_online.text = "Online"
        }
        //匹配度
        id_tv_user_details_percentage.text = result.matchingDegree ?: "0%"
        //自我介绍
        id_tv_user_details_introduction.text = result.description ?: ""
        //是否点击了喜欢
        isLike = result.hasLikes.equals("1")
        likeCallBack(isLike)
        //喜欢的数量
        id_tv_user_details_like_num.text = result.likes ?: "0"
//        if (isLike) {
//            id_img_user_details_like.setImageResource(R.drawable.icon_like_click_user2)
//            id_tv_user_details_like_num.setTextColor(Color.parseColor("#E838AC"))
//        } else {
//            id_img_user_details_like.setImageResource(R.drawable.icon_like_default_user2)
//            id_tv_user_details_like_num.setTextColor(Color.parseColor("#666666"))
//        }
        //tag
        setPersonalTagUI(result.tags?.split('|'))
        //视频价格
        id_tv_user_details_call_price.text = "${result.videoPrice}/min"

        mPhotoAdapter?.clear()
        mPhotoAdapter?.appendToList(result.picturesList)

        //
        if (!result.picturesList.isNullOrEmpty()) {
            onItemClick(id_img_user_details_photo, 0, result.picturesList!![0])
        }
    }

    private var isLike = false;
    override fun likeCallBack(boolean: Boolean) {
        if (isLike) {
            var i = id_tv_user_details_like_num.text.toString().toInt() + 1
            id_tv_user_details_like_num.text = i.toString()
            id_img_user_details_like.setImageResource(R.drawable.icon_like_click_user2)
            id_tv_user_details_like_num.setTextColor(Color.parseColor("#E838AC"))
        } else {
            var i = id_tv_user_details_like_num.text.toString().toInt() - 1
            if (i < 0) {
                i = 0;
            }
            id_tv_user_details_like_num.text = i.toString()
            id_img_user_details_like.setImageResource(R.drawable.icon_like_default_user2)
            id_tv_user_details_like_num.setTextColor(Color.parseColor("#666666"))
        }
    }

    override fun blackCallBack(boolean: Boolean) {
        if(boolean){
           showToast("Add to Blacklist Successful")
            finish()
        }
    }

    override fun reportCallback(boolean: Boolean) {
        if(boolean){
            showToast("Report Success")
        }
    }

    override fun onItemClick(view: View?, position: Int, data: String?) {
        mPhotoAdapter?.setSelectPhoto(position)
        id_sfv_user_detail_video.visibility = View.INVISIBLE
        id_img_user_details_photo.visibility = View.VISIBLE
        id_img_user_details_photo.setLoadImageUrl(data, true)
        //Video
//        if (position == 1) {
//            //http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4
//            id_sfv_user_detail_video.visibility = View.GONE
//            id_img_user_details_photo.visibility = View.VISIBLE
////            id_img_user_details_photo.setLoadImageUrl("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4", true)
////            playUserVideo("http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4")
////            playUserVideo("https://media.w3.org/2010/05/sintel/trailer.mp4")
//        } else {
//            id_sfv_user_detail_video.visibility = View.INVISIBLE
//            id_img_user_details_photo.visibility = View.VISIBLE
//            id_img_user_details_photo.setLoadImageUrl(data, true)
//        }
    }

    var player: ExoPlayer? = null
    private fun playUserVideo(url: String) {
        if (player == null) {
            player = ExoPlayer.Builder(this).build()
            id_sfv_user_detail_video.player = player
        }
//        id_sfv_user_detail_video.controllerAutoShow = false
//        id_sfv_user_detail_video.controllerHideOnTouch = false
//        id_sfv_user_detail_video.hideController()
        // Build the media item.
        val mediaItem: MediaItem = MediaItem.fromUri(Uri.parse(url))
        player?.setMediaItem(mediaItem)
        player?.prepare()
        player?.playWhenReady = true
    }

    //设置标签UI
    private fun setPersonalTagUI(tagList: List<String>?) {
        if (tagList.isNullOrEmpty()) {
            return
        }
        id_fl_tag_list_view.removeAllViews()
        for (tagStr in tagList) {
            var tagView = TextView(this)
            tagView.text = tagStr
            tagView.textSize = 14f
            tagView.setTextColor(Color.parseColor("#824FFF"))
            tagView.background = resources.getDrawable(R.drawable.shape_eae1ff_r3)
            tagView.setPadding(Utils.dip2px(5), Utils.dip2px(2), Utils.dip2px(5), Utils.dip2px(2))
            id_fl_tag_list_view.addView(tagView)
        }
    }

//    private var mReportPop: PopupWindow? = null;
//    private var popupWindow: PopupWindow? = null;

    //弹出举报弹窗
//    private fun showReportPop() {
//        val popView = LayoutInflater.from(this).inflate(
//            R.layout.pop_user_details_report_list,
//            null
//        )
//        if (mReportPop == null) {
//            mReportPop = object : PopupWindow(
//                popView,
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT,
//                false
//            ) {
//                override fun showAtLocation(parent: View?, gravity: Int, x: Int, y: Int) {
//                    if (this@UserDetailsActivity.isFinishing) {
//                        val dialogWindow: Window = this@UserDetailsActivity.window
//                        val lp = dialogWindow.attributes // 获取对话框当前的参数值
//                        lp.alpha = 0.5f
//                        dialogWindow.attributes = lp
//                    }
//                    super.showAtLocation(parent, gravity, x, y)
//                }
//
//                override fun dismiss() {
//                    if (this@UserDetailsActivity.isFinishing) {
//                        val dialogWindow: Window = this@UserDetailsActivity.getWindow()
//                        val lp = dialogWindow.attributes // 获取对话框当前的参数值
//                        lp.alpha = 1.0f
//                        dialogWindow.attributes = lp
//                    }
//                    super.dismiss()
//                }
//            }
//        }
//
//        mReportPop?.setBackgroundDrawable(BitmapDrawable())
//
//        popView.findViewById<TextView>(R.id.id_tv_pop_report_add_to_black).setOnClickListener {
//            mReportPop?.dismiss()
//        }
//        popView.findViewById<TextView>(R.id.id_tv_pop_delete).setOnClickListener {
//            mReportPop?.dismiss()
//        }
//        popView.findViewById<TextView>(R.id.id_tv_pop_report).setOnClickListener {
//            mReportPop?.dismiss()
//        }
//        popView.findViewById<TextView>(R.id.id_tv_pop_cancel).setOnClickListener {
//            mReportPop?.dismiss()
//        }
//
//        mReportPop?.isFocusable = true
//        mReportPop?.isTouchable = true
//        mReportPop?.isOutsideTouchable = false
//
//        mReportPop?.showAtLocation(id_img_btn_report, Gravity.BOTTOM, 0, 0)
//    }


}