package com.zxhl.main.page.view


import android.app.Activity
import android.app.Dialog
import android.content.Context
import android.os.Build
import android.util.Log
import android.view.Gravity
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.RequiresApi
import com.cncoderx.wheelview.OnWheelChangedListener
import com.cncoderx.wheelview.WheelView
import com.loper7.date_time_picker.DateTimePicker
import com.loper7.date_time_picker.StringUtils
import com.zxhl.cms.AppContext
import com.zxhl.cms.utils.BirthdayToAgeUtil
import com.zxhl.cms.utils.PhoneUtils
import com.zxhl.cms.utils.ReporContentListener
import com.zxhl.main.R
import com.zxhl.main.page.*
import com.zxhl.main.page.`interface`.*
import java.time.LocalDate
import java.util.*


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

        val mDialogView = View.inflate(activity, layout, null)
        setHeadDialog.setContentView(mDialogView)
        val window = setHeadDialog.window
        window?.setGravity(gravity)
        val lp = window?.attributes
        window?.decorView?.setPadding(0, 0, 0, 0)
        lp?.width = PhoneUtils.getScreenWidth(AppContext.get())
        if (gravity == Gravity.TOP) {
            lp?.height = PhoneUtils.getScreenHeight(AppContext.get())
        }
        window?.attributes = lp
        if (animation > 0)
            window?.setWindowAnimations(animation)//设置动画
        return mDialogView
    }

    fun showFirstGuide(
        context: Activity,
        listener: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_newpeople,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val img_free_get = mDialogView.findViewById<TextView>(R.id.id_img_free_get)

        img_free_get.setOnClickListener {
            mDialog.dismiss()
            //  SettingPreference.setShowGuide()
            listener.onClick(it)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog?.show()
    }

    fun showPictureOrPhoto(
        context: Activity,
        listener: View.OnClickListener,
        listener2: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_photo,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        val tv_paizhap = mDialogView.findViewById<TextView>(R.id.id_tv_paizhao)
        val tv_xiangce = mDialogView.findViewById<TextView>(R.id.id_tv_xiangce)
        val tv_cance = mDialogView.findViewById<TextView>(R.id.id_tv_cance)
        tv_paizhap?.setOnClickListener {
            listener.onClick(it)
            mDialog.dismiss()
        }
        tv_xiangce?.setOnClickListener {
            listener2.onClick(it)
            mDialog.dismiss()
        }
        tv_cance?.setOnClickListener {
            mDialog.dismiss()
        }
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun showchooseDate(
        context: Activity,
        chooseDatalistener: ChooseDateCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_date,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseDate: String? = ""

        val datePicker = mDialogView.findViewById<DateTimePicker>(R.id.dateTimePicker)
        val _MAX_TIME = (365 * 24 * 60 * 60 * 13 * 1000L) + (366 * 24 * 60 * 60 * 5 * 1000L)
        val curTime = System.currentTimeMillis()
        val maxTime: Long = curTime
        datePicker?.setDefaultMillisecond(curTime)
        datePicker?.setMaxMillisecond(maxTime)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        //  datePicker?.setMaxMillisecond()
        datePicker.setOnDateTimeChangedListener(object : DateTimePicker.OnDateTimeChangedListener {
            override fun onDateTimeChanged(view: DateTimePicker?, millisecond: Long) {
                chooseDate = StringUtils.conversionTime(millisecond, "yyyy-MM-dd")
            }
        })
        tvsure?.setOnClickListener {

            chooseDatalistener?.chooseData(BirthdayToAgeUtil.BirthdayToAge(chooseDate))
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseDate = ""
            chooseDatalistener?.chooseData(chooseDate)
            mDialog.dismiss()
        }

        val array = intArrayOf(0, 1, 2)
        datePicker?.setDisplayType(array)
        datePicker?.showLabel(false)
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun showchooseHeight(
        context: Activity,
        chooseDatalistener: ChooseHeightCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_height,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseData: String? = "160cm以下"
        val wheelview = mDialogView.findViewById<WheelView>(R.id.wheel3d)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        wheelview.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onChanged(view: WheelView?, oldIndex: Int, newIndex: Int) {
                chooseData = view!!.getItem(newIndex).toString()
                Log.e("MXL", "chooseData:" + chooseData)
            }

        })
        tvsure?.setOnClickListener {
            chooseDatalistener?.chooseHeight(chooseData)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseData = ""
            chooseDatalistener?.chooseHeight(chooseData)
            mDialog.dismiss()
        }

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


    fun showchooseWeight(
        context: Activity,
        chooseDatalistener: ChooseWeightCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_weight,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseData: String? = "40kg以下"
        val wheelview = mDialogView.findViewById<WheelView>(R.id.wheel3d)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        wheelview.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onChanged(view: WheelView?, oldIndex: Int, newIndex: Int) {
                chooseData = view!!.getItem(newIndex).toString()
                Log.e("MXL", "chooseData:" + chooseData)
            }

        })
        tvsure?.setOnClickListener {
            chooseDatalistener?.chooseWeight(chooseData)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseData = ""
            chooseDatalistener?.chooseWeight(chooseData)
            mDialog.dismiss()
        }

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

    fun showchooseIncome(
        context: Activity,
        chooseDatalistener: ChooseIncomeCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_income,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseData: String? = "10万以下"
        val wheelview = mDialogView.findViewById<WheelView>(R.id.wheel3d)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        wheelview.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onChanged(view: WheelView?, oldIndex: Int, newIndex: Int) {
                chooseData = view!!.getItem(newIndex).toString()
                Log.e("MXL", "chooseData:" + chooseData)
            }

        })
        tvsure?.setOnClickListener {
            chooseDatalistener?.chooseIncom(chooseData)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseData = ""
            chooseDatalistener?.chooseIncom(chooseData)
            mDialog.dismiss()
        }

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

    fun showchooseoccupation(
        context: Activity,
        chooseDatalistener: ChossoccupationCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_zhiye,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseData: String? = "销售/业务"
        val wheelview = mDialogView.findViewById<WheelView>(R.id.wheel3d)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        wheelview.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onChanged(view: WheelView?, oldIndex: Int, newIndex: Int) {
                chooseData = view!!.getItem(newIndex).toString()
                Log.e("MXL", "chooseData:" + chooseData)
            }

        })
        tvsure?.setOnClickListener {
            chooseDatalistener?.chooseOccupation(chooseData)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseData = ""
            chooseDatalistener?.chooseOccupation(chooseData)
            mDialog.dismiss()
        }

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

    fun showchooseGender(
        context: Activity,
        chooseDatalistener: ChooseGenderCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_gender,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )
        var chooseData: String? = "Girl"
        val wheelview = mDialogView.findViewById<WheelView>(R.id.wheel3d)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        wheelview.setOnWheelChangedListener(object : OnWheelChangedListener {
            override fun onChanged(view: WheelView?, oldIndex: Int, newIndex: Int) {
                chooseData = view!!.getItem(newIndex).toString()
                Log.e("MXL", "chooseData:" + chooseData)
            }

        })
        tvsure?.setOnClickListener {
            chooseDatalistener?.chooseGender(chooseData)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseData = ""
            chooseDatalistener?.chooseGender(chooseData)
            mDialog.dismiss()
        }

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

    fun showMemberTrip(
        context: Activity
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_trip,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val tv_xingbi_ji_suo = mDialogView.findViewById<TextView>(R.id.id_tv_xingbi_jiesuo)
        val tv_vip = mDialogView.findViewById<TextView>(R.id.id_tv_vip)
        mDialogView.findViewById<ImageView>(R.id.id_img_close)?.setOnClickListener {
            mDialog.dismiss()
        }
        tv_xingbi_ji_suo.setOnClickListener {
            mDialog.dismiss()
        }
        tv_vip?.setOnClickListener {
            mDialog.dismiss()
        }

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

    fun showCanleFollow(
        context: Activity,
        listener: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_cance_follow,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val tv_quxiao = mDialogView.findViewById<TextView>(R.id.id_tv_quxiao)
        val tv_sure = mDialogView.findViewById<TextView>(R.id.id_tv_sure)
        mDialogView.findViewById<ImageView>(R.id.id_img_close)?.setOnClickListener {
            mDialog.dismiss()
        }
        tv_quxiao.setOnClickListener {
            mDialog.dismiss()
        }
        tv_sure?.setOnClickListener {
            mDialog.dismiss()
            listener.onClick(it)
        }

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

    //举报
    fun showReport(
        context: Activity,
        listener: ReportListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_report,
                context,
                mDialog,
                0,
                Gravity.BOTTOM
            )

        mDialog.findViewById<TextView>(R.id.id_tv_add_to_blacklist).setOnClickListener {
            mDialog.dismiss()
            listener.report(0)
        }
        mDialog.findViewById<TextView>(R.id.id_tv_delete).setOnClickListener {
            mDialog.dismiss()
            listener.report(1)
        }
        mDialog.findViewById<TextView>(R.id.id_tv_report).setOnClickListener {
            mDialog.dismiss()
            listener.report(2)
        }
        mDialog.findViewById<TextView>(R.id.id_tv_cancel).setOnClickListener {
            mDialog.dismiss()
        }
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    fun showchooseConsellation(
        context: Activity,
        chooseDatalistener: ChooseDateCallBack
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_choose_constellation,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        var chooseDate: String? = ""

        val datePicker = mDialogView.findViewById<DateTimePicker>(R.id.dateTimePicker)
        val _MAX_TIME = (365 * 24 * 60 * 60 * 13 * 1000L) + (366 * 24 * 60 * 60 * 5 * 1000L)
        val curTime = System.currentTimeMillis()
        val maxTime: Long = curTime
        datePicker?.setDefaultMillisecond(curTime)
        datePicker?.setMaxMillisecond(maxTime)
        val tvsure = mDialogView.findViewById<TextView>(R.id.id_tv_confirm)
        val tvquxiao = mDialogView.findViewById<TextView>(R.id.id_tv_cancel)
        //  datePicker?.setMaxMillisecond()
        datePicker.setOnDateTimeChangedListener(object : DateTimePicker.OnDateTimeChangedListener {
            override fun onDateTimeChanged(view: DateTimePicker?, millisecond: Long) {
                chooseDate = StringUtils.conversionTime(millisecond, "yyyy-MM-dd")
            }
        })
        tvsure?.setOnClickListener {

            chooseDatalistener?.chooseData(chooseDate)
            mDialog.dismiss()
        }
        tvquxiao?.setOnClickListener {
            chooseDate = ""
            chooseDatalistener?.chooseData(chooseDate)
            mDialog.dismiss()
        }

        val array = intArrayOf(0, 1, 2)
        datePicker?.setDisplayType(array)
        datePicker?.showLabel(false)
        mDialog.setCanceledOnTouchOutside(true)
        mDialog.setCancelable(true)
        mDialog?.show()
    }

    //黑名单警告
    fun showBlackWarning(
        context: Activity,
        listener: View.OnClickListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_block_waring,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val tv_back = mDialogView.findViewById<TextView>(R.id.id_tv_back)
        val tv_cancellation = mDialogView.findViewById<TextView>(R.id.id_tv_cancellation)
        tv_back.setOnClickListener {
            mDialog.dismiss()
        }
        tv_cancellation?.setOnClickListener {
            mDialog.dismiss()
            listener.onClick(it)
        }

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

    fun showReportContent(
        context: Activity,
        listener: ReporContentListener
    ) {
        if (context == null) return
        val mDialog = Dialog(context, R.style.CenterCompatDialogTheme)
        val mDialogView =
            showBottomDialog(
                R.layout.dialog_layout_report_content,
                context,
                mDialog,
                0,
                Gravity.CENTER
            )
        val id_ll_advertising = mDialogView.findViewById<LinearLayout>(R.id.id_ll_advertising)
        val id_img_advertising = mDialogView.findViewById<ImageView>(R.id.id_img_advertising)
        val id_ll_harass_me = mDialogView.findViewById<LinearLayout>(R.id.id_ll_harass_me)
        val id_img_harass_me = mDialogView.findViewById<ImageView>(R.id.id_img_harass_me)
        val id_ll_copycat = mDialogView.findViewById<LinearLayout>(R.id.id_ll_copycat)
        val id_img_copy_cat=mDialogView.findViewById<ImageView>(R.id.id_img_copy_cat)
        val id_ll_lllegal = mDialogView.findViewById<LinearLayout>(R.id.id_ll_lllegal)
        val id_img_lllegal=mDialogView.findViewById<ImageView>(R.id.id_img_lllegal)
        val id_ll_sexual = mDialogView.findViewById<LinearLayout>(R.id.id_ll_sexual)
        val id_img_sexual=mDialogView.findViewById<ImageView>(R.id.id_img_sexual)
        val id_ll_other = mDialogView.findViewById<LinearLayout>(R.id.id_ll_other)
        val id_img_other=mDialogView.findViewById<ImageView>(R.id.id_img_other)
        val tv_back = mDialogView.findViewById<TextView>(R.id.id_tv_back)
        val tv_continue = mDialogView.findViewById<TextView>(R.id.id_tv_continue)
        var reportContent:String="Advertising"
        tv_back.setOnClickListener {
            mDialog.dismiss()
        }
        tv_continue?.setOnClickListener {
            mDialog.dismiss()
            listener.toReport(reportContent)
        }
        id_ll_advertising.setOnClickListener {
            reportContent="Advertising"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4_2)
        }
        id_ll_harass_me.setOnClickListener {
            reportContent="Harass me"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4_2)
        }
        id_ll_copycat.setOnClickListener {
            reportContent="Copycat"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4_2)
        }
        id_ll_lllegal.setOnClickListener {
            reportContent="Illegal or criminal"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4_2)
        }
        id_ll_sexual.setOnClickListener {
            reportContent="Sexual hint"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4_2)
        }
        id_ll_other.setOnClickListener {
            reportContent="Other"
            id_img_advertising.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_harass_me.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_copy_cat.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_lllegal.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_sexual.setImageResource(R.drawable.icon_weixuan_zise4_2)
            id_img_other.setImageResource(R.drawable.icon_weixuan_zise4)
        }
        mDialog.setCanceledOnTouchOutside(false)
        mDialog.setCancelable(false)
        mDialog?.show()
    }
}