package com.duben.microtribe.ui.widgets;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager.LayoutParams;
import android.widget.ImageView;
import android.widget.TextView;

import com.duben.microtribe.R;

/**
 * 仿苹果自定义dialog选择框
 */
public class PayDialog extends Dialog {
    private Context context;
    private LayoutParams lp;
    private DialogListener listener;

    private TextView tv_dialogpay_pay;
    private ImageView iv_dialogpay_cancel;
    private ImageView iv_dialogpay_alipay;
    private ImageView iv_dialogpay_wx;

    private String payChannel = "alipay";

    public PayDialog(Context context, DialogListener listener) {
        super(context, R.style.dialog);
        this.context = context;
        this.listener = listener;
        setContentView(R.layout.dialog_pay);
        // 设置window属性
        lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        lp.width = LayoutParams.MATCH_PARENT;
        lp.windowAnimations = R.style.DialogAnimBottom;
//		lp.dimAmount = 0; // 去背景遮盖
        // lp.alpha = 1.0f;//透明效果
        getWindow().setAttributes(lp);

        // 设置外部不可关闭
        setCancelable(false);
        setCanceledOnTouchOutside(false);
        setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
                if (i == keyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });

        // 查找View
        tv_dialogpay_pay = (TextView) findViewById(R.id.tv_dialogpay_pay);
        iv_dialogpay_cancel = (ImageView) findViewById(R.id.iv_dialogpay_cancel);
        iv_dialogpay_alipay = (ImageView) findViewById(R.id.iv_dialogpay_alipay);
        iv_dialogpay_wx = (ImageView) findViewById(R.id.iv_dialogpay_wx);

        tv_dialogpay_pay.setOnClickListener(listener);
        iv_dialogpay_cancel.setOnClickListener(listener);

        findViewById(R.id.ll_dialogpay_alipay).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setAlipay();
            }
        });

        findViewById(R.id.ll_dialogpay_wx).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setWx();
            }
        });
    }

    public void setAlipay() {
        payChannel = "alipay";
        iv_dialogpay_alipay.setImageResource(R.mipmap.ic_vip_enable);
        iv_dialogpay_wx.setImageResource(R.mipmap.ic_vip_none);
    }

    public void setWx() {
        payChannel = "weixin";
        iv_dialogpay_alipay.setImageResource(R.mipmap.ic_vip_none);
        iv_dialogpay_wx.setImageResource(R.mipmap.ic_vip_enable);
    }

    public String getPayChannel() {
        return payChannel;
    }
}
