package com.zxhl.cms.widget

import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.text.TextUtils
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.RelativeLayout
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.request.RequestOptions
import com.zxhl.cms.AppContext
import com.zxhl.cms.R
import com.zxhl.cms.common.Constant
import com.zxhl.cms.net.ApiClient
import com.zxhl.cms.net.RxSchedulers
import com.zxhl.cms.net.callback.BaseObserver
import com.zxhl.cms.net.model.box.HomeListEntity
import com.zxhl.cms.net.model.box.RoomMemberEntity
import com.zxhl.cms.utils.*

object ChatRoomDialogUtils {
    fun showBottomDialog(
        layout: Int,
        activity: Context,
        setHeadDialog: Dialog,
        animation: Int,
        gravity: Int
    ): View {

        // AlertDialog setHeadDialog = new AlertDialog.Builder(activity).create();
        // setHeadDialog.show();

        val mDialogView = View.inflate(activity, layout, null)
        setHeadDialog.setContentView(mDialogView)
        val window = setHeadDialog.window
        // window.setContentView(mDialogView);
        window?.setGravity(gravity)
//        window.setWindowAnimations(R.style.AnimBottom)
        // WindowManager windowManager = activity.getWindowManager();
        // Display display = windowManager.getDefaultDisplay();
        val lp = window?.attributes
        window?.decorView?.setPadding(0, 0, 0, 0)
        // 设置宽度
        //Point size = new Point();
        //display.getSize(size);
        //lp.width = size.x;
        lp?.width = PhoneUtils.getScreenWidth(AppContext.get())
        if (gravity == Gravity.TOP) {
            lp?.height = PhoneUtils.getScreenHeight(AppContext.get())
        }
        //lp.width = (int) (display.getWidth()); // 设置宽度方式二: @过时
        //window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,0);
        window?.attributes = lp
        if (animation > 0)
            window?.setWindowAnimations(animation)//设置动画
        return mDialogView
    }

    fun showCloseRoom(activity: Context, num: Int, listener: View.OnClickListener?) {
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_close_room,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        val tvcreatetime = mDialogView.findViewById<TextView>(R.id.id_tv_creat_times)
        tvcreatetime?.text = "今日剩余创建次数${num}次"
        mDialogView.findViewById<TextView>(R.id.id_tv_close_room).setOnClickListener {
            mDialog.dismiss()
            listener?.onClick(it)
        }

        mDialogView.findViewById<TextView>(R.id.id_tv_go_on_chat).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog?.show()
    }

    fun showBottomMenu(activity: Activity, listener: Bottomlistener) {
        val mDialog = Dialog(activity, R.style.top_theme_dialog)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_bottom_menu,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_one).setOnClickListener {
            listener.clickfunction(1)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_two).setOnClickListener {
            listener.clickfunction(2)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_three).setOnClickListener {
            listener.clickfunction(3)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_four).setOnClickListener {
            listener.clickfunction(4)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_five).setOnClickListener {
            listener.clickfunction(5)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_six).setOnClickListener {
            listener.clickfunction(6)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_seven).setOnClickListener {
            listener.clickfunction(7)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_eight).setOnClickListener {
            listener.clickfunction(8)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_nine).setOnClickListener {
            listener.clickfunction(9)
        }

        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //
    fun showMicControl(activity: Context, listener: MicControlListener) {
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_mic_control,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        val tvneedmic = mDialogView.findViewById<TextView>(R.id.id_tv_need_mic)
        val tvnomic = mDialogView.findViewById<TextView>(R.id.id_tv_no_mic)
        var ischeck: Boolean = true
        tvneedmic?.setOnClickListener {

            tvneedmic.setBackgroundResource(R.drawable.shape_bg_mic_select)
            tvnomic?.setBackgroundResource(R.drawable.shape_bg_mic_defalt)
            ischeck = true

        }
        tvnomic?.setOnClickListener {

            tvneedmic.setBackgroundResource(R.drawable.shape_bg_mic_defalt)
            tvnomic?.setBackgroundResource(R.drawable.shape_bg_mic_select)
            ischeck = false

        }
        mDialogView.findViewById<ImageView>(R.id.id_img_close).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_sure).setOnClickListener {
            listener.setMicControl(ischeck)
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun setPrivateRoom(activity: Context, listener: View.OnClickListener) {
        val mDialog = Dialog(activity, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_private_room,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )


        mDialogView.findViewById<TextView>(R.id.id_tv_cance).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_sure).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //向房主申请上麦
    fun showApplyForWheat(context: Context, seatnumber: String, listener: View.OnClickListener) {
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_for_wheat,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        val tvSeatNum = mDialogView.findViewById<TextView>(R.id.id_tv_seat_num)
        tvSeatNum?.text = "向房主申请上${seatnumber}号麦位聊天"
        mDialogView.findViewById<TextView>(R.id.id_tv_cance).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_sure).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //被踢用户不可进入房间
    fun showNoEntryRomm(context: Context) {
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_no_entry_room,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )

        mDialogView.findViewById<TextView>(R.id.id_tv_cance).setOnClickListener {
            mDialog.dismiss()
        }
        mDialogView.findViewById<TextView>(R.id.id_tv_sure).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //聊天室分享
    fun showShareFriendToRoom(context: Context,list:List<RoomMemberEntity>) {
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_share_friend_room,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val id_rl_friend_chat=mDialogView.findViewById<RecyclerView>(R.id.id_rl_friend_chat)
        var mAdapter =
            ChatFriendAdapter(context, object : OnRecycleItemClickListener<RoomMemberEntity> {
                override fun onItemClick(view: View?, position: Int, data: RoomMemberEntity?) {
                    when (view?.id) {
                        R.id.id_item->{
                            mDialog.dismiss()
                            RxBus.get().post(Constant.SWITCH_PAGE, 2)
                        }

                    }

                }

            })
        mDialogView.findViewById<RelativeLayout>(R.id.id_rl_layout).setOnClickListener {
            mDialog.dismiss()
        }
        id_rl_friend_chat.layoutManager=LinearLayoutManager(context,RecyclerView.HORIZONTAL,false)
        id_rl_friend_chat.adapter=mAdapter
        mAdapter.appendToList(list)
        mAdapter.notifyDataSetChanged()
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //聊天室内查看用户信息
    fun showLookUserInfo(
        context: Context,
        AudienceClick: Int,
        isowner: Boolean,
        info: HomeListEntity,
        listener: UserInfoListener
    ) {
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_look_info,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<RelativeLayout>(R.id.id_rl_info).setOnClickListener {
            mDialog.dismiss()
        }
        val id_tv_user_id = mDialogView.findViewById<TextView>(R.id.id_tv_user_id)
        val id_tv_name = mDialogView.findViewById<TextView>(R.id.id_tv_name)//姓名
        val id_tv_sign = mDialogView.findViewById<TextView>(R.id.id_tv_sign)//个性签名
        val id_tv_to_gift = mDialogView.findViewById<TextView>(R.id.id_tv_to_gift)//送礼物
        val id_tv_guanzhu = mDialogView.findViewById<TextView>(R.id.id_tv_guanzhu)//是否关注
        val id_tv_alt_it = mDialogView.findViewById<TextView>(R.id.id_tv_alt_it)//@他
        val id_tv_message = mDialogView.findViewById<TextView>(R.id.id_tv_message)//发消息
        val id_tv_turn_on_mic = mDialogView.findViewById<TextView>(R.id.id_tv_turn_on_mic)//闭麦
        val id_tv_off_seat = mDialogView.findViewById<TextView>(R.id.id_tv_off_seat)//抱下座位
        val id_tv_invite_mic = mDialogView.findViewById<TextView>(R.id.id_tv_invite_mic)//邀请上麦
        val id_tv_kick_out = mDialogView.findViewById<TextView>(R.id.id_tv_kick_out)//踢出
        val id_user_head = mDialogView.findViewById<ImageView>(R.id.id_user_head)//头像
        val id_img_sex = mDialogView.findViewById<ImageView>(R.id.id_img_sex)//性别
        if (isowner) {
            id_tv_kick_out.visibility = View.VISIBLE
            //房主看用户信息
            if (AudienceClick == 0) {
                id_tv_invite_mic.visibility = View.VISIBLE
                id_tv_turn_on_mic.visibility = View.GONE
                id_tv_off_seat.visibility = View.GONE
            } else {
                id_tv_invite_mic.visibility = View.GONE
                id_tv_turn_on_mic.visibility = View.VISIBLE
                id_tv_off_seat.visibility = View.VISIBLE
            }
        } else {
            id_tv_kick_out.visibility = View.GONE
            id_tv_invite_mic.visibility = View.GONE
            id_tv_turn_on_mic.visibility = View.GONE
            id_tv_off_seat.visibility = View.GONE
        }
        id_tv_alt_it.text = "@他"
        id_tv_user_id.text = "ID：${info.uid}"
        id_tv_name.text = info.nickname
        id_tv_sign.text = info.description
        if (TextUtils.equals(info.sex, "1")) {
            id_img_sex.setImageResource(R.drawable.icon_nan_da)
        } else {
            id_img_sex.setImageResource(R.drawable.icon_nv_da)
        }
        if (TextUtils.equals(info.hasFollows, "0")) {
            id_tv_guanzhu.text = "关注"
        } else {
            id_tv_guanzhu.text = "已关注"
        }
        if (!TextUtils.isEmpty(info.headImage)) {
            Glide.with(AppContext.get()).load(info.headImage)
                .apply(RequestOptions.bitmapTransform(CircleCrop()))
                .into(id_user_head)
        } else {
            id_user_head.setImageResource(R.drawable.icon_user_photo_default)
        }
        id_tv_to_gift.setOnClickListener {
            mDialog.dismiss()
            listener.toGift(
                info.uid.toString(),
                info.nickname.toString(),
                info.headImage.toString(),
                info.hasFollows.toString()
            )
        }
        id_tv_guanzhu.setOnClickListener {
            if (TextUtils.equals(info.hasFollows, "0")) {
                listener.follow(info.uid)
                info.hasFollows = "1"
                id_tv_guanzhu.text = "已关注"
            }

        }
        id_tv_alt_it.setOnClickListener {
            mDialog.dismiss()
        }
        id_tv_message.setOnClickListener {
            listener.sendMsg(info.uid, info.chatStatus, info.nickname)

        }
        id_tv_turn_on_mic.setOnClickListener {
            mDialog.dismiss()
            listener.offMic(info.uid)
        }

        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun showAudienceMenu(activity: Activity, listener: Bottomlistener) {
        val mDialog = Dialog(activity, R.style.top_theme_dialog)
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_audience_menu,
                activity,
                mDialog,
                0,
                Gravity.CENTER
            )
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_one).setOnClickListener {
            listener.clickfunction(1)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_two).setOnClickListener {
            listener.clickfunction(2)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_three).setOnClickListener {
            listener.clickfunction(3)
        }
        mDialogView.findViewById<LinearLayout>(R.id.id_ll_four).setOnClickListener {
            listener.clickfunction(4)
        }

        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun showRoomMember(context: Context, list: List<RoomMemberEntity>) {
        val mDialog = Dialog(context, R.style.UpdateVersionCompatDialogTheme)
        var mAdapter =
            RoomMemberAdapter(context, object : OnRecycleItemClickListener<RoomMemberEntity> {
                override fun onItemClick(view: View?, position: Int, data: RoomMemberEntity?) {
                    when (view?.id) {
                        R.id.id_rl_room_member_item->{
                            JumpUtils.UserDetailJump(data?.uid,"0")
                        }
                        R.id.id_tv_follow->{
                            //关注
                            followIt(data?.uid)
                        }
                    }

                }

            })
        val mDialogView =
            CenterDialog.showBottomDialog(
                R.layout.dialog_layout_room_member,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val rlmember = mDialogView.findViewById<RecyclerView>(R.id.id_rl_room_member)
        rlmember.layoutManager = LinearLayoutManager(context)
        rlmember.adapter = mAdapter
        mAdapter?.appendToList(list)
        mAdapter?.notifyDataSetChanged()
        mDialogView.findViewById<RelativeLayout>(R.id.id_rl_layout).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog?.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(true)
        mDialog?.show()
    }
    fun followIt(uid:String?){
        ApiClient.homeApi.followUser(uid).compose(RxSchedulers.observableIO2Main())
            .subscribe(object : BaseObserver<Any>() {
                override fun onSuccess(result: Any?) {
                    Utils.showToast(AppContext.get(),"关注成功,要多和Ta一起来语音房玩哦~")
                }

                override fun onFailure(e: Throwable?, code: String?, errorMsg: String?) {
                    Utils.showToast(AppContext.get(),"关注失败,请再次重试哦~")
                }
            })
    }
}