package com.duben.happyplaylet.ui.widgets

import android.app.Dialog
import android.content.Context
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.WindowManager
import android.widget.FrameLayout
import android.widget.ImageView
import android.widget.RelativeLayout
import android.widget.TextView
import com.duben.happyplaylet.R
import com.duben.happyplaylet.common.AppConfig
import com.duben.happyplaylet.mvp.model.VipBean

class PayYesDialog(
    private val context: Context,
    vipBean: VipBean.ListBean,
    private val listener: DialogListener
) : Dialog(
    context, R.style.dialog
) {
    private val lp: WindowManager.LayoutParams
    private val fl_dialog_vip_money_root: FrameLayout
    private val tv_dialog_vip_next: TextView
    private val tv_dialog_vip_money_left: TextView
    private val tv_dialog_vip_money_right: TextView
    private val tv_dialog_vip_money_center: TextView
    private val iv_dialog_vip_quit: ImageView
    private val rl_dialog_vip_bg: RelativeLayout

    init {
        setContentView(R.layout.dialog_payyes)
        // 设置window属性
        lp = window!!.attributes
        lp.gravity = Gravity.CENTER
        lp.width = WindowManager.LayoutParams.MATCH_PARENT
        lp.windowAnimations = R.style.DialogAnimBottom
        window!!.attributes = lp

        // 设置外部不可关闭
        setCancelable(false)
        setCanceledOnTouchOutside(false)
        setOnKeyListener { dialogInterface, i, keyEvent ->
            if (i == KeyEvent.KEYCODE_BACK) {
                true
            } else false
        }


        // 查找View
        fl_dialog_vip_money_root = findViewById<View>(R.id.fl_dialog_vip_money_root) as FrameLayout
        iv_dialog_vip_quit = findViewById<View>(R.id.iv_dialog_vip_quit) as ImageView
        tv_dialog_vip_next = findViewById<View>(R.id.tv_dialog_vip_next) as TextView
        tv_dialog_vip_money_left = findViewById<View>(R.id.tv_dialog_vip_money_left) as TextView
        tv_dialog_vip_money_right = findViewById<View>(R.id.tv_dialog_vip_money_right) as TextView
        tv_dialog_vip_money_center = findViewById<View>(R.id.tv_dialog_vip_money_center) as TextView
        rl_dialog_vip_bg = findViewById<View>(R.id.rl_dialog_vip_bg) as RelativeLayout
        iv_dialog_vip_quit.setOnClickListener(listener)
        tv_dialog_vip_next.setOnClickListener(listener)

        if (vipBean.trial == 0) {
            //0-没签约 无0.01
            fl_dialog_vip_money_root.visibility = View.VISIBLE
            tv_dialog_vip_money_left.text = "￥${vipBean.oldPrice}"
            tv_dialog_vip_money_right.text = "￥${vipBean.oldPrice}"
            tv_dialog_vip_money_center.text = "￥${vipBean.price}"
            tv_dialog_vip_next.text = "立即开通"
            rl_dialog_vip_bg.setBackgroundResource(R.mipmap.bg_vip_dialog_one)
        } else {
            fl_dialog_vip_money_root.visibility = View.GONE
            tv_dialog_vip_next.text = "${0.01}元试用"
            rl_dialog_vip_bg.setBackgroundResource(R.mipmap.bg_vip_dialog_zero)
        }
    }
}