package com.duben.microtribe.ui.widgets;

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

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.duben.microtribe.R;
import com.duben.microtribe.manager.UserManager;


/**
 * 支付返回弹窗
 */
public class BackPayDialog extends Dialog {
    private Context context;
    private LayoutParams lp;
    private DialogListener listener;

    private FrameLayout tv_dialog_backpay;
    private TextView tv_vip_next;
    private ImageView iv_vip_new;
    private TextView iv_dialog_backpay_cancel;

    public BackPayDialog(Context context, DialogListener listener) {
        super(context, R.style.dialog);
        this.context = context;
        this.listener = listener;
        setContentView(R.layout.dialog_backpay);
        // 设置window属性
        lp = getWindow().getAttributes();
        lp.gravity = Gravity.BOTTOM;
        lp.width = LayoutParams.MATCH_PARENT;
        lp.windowAnimations = R.style.DialogAnimBottom;
        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_dialog_backpay = (FrameLayout) findViewById(R.id.tv_dialog_backpay);
        tv_vip_next = (TextView) findViewById(R.id.tv_vip_next);
        iv_vip_new = (ImageView) findViewById(R.id.iv_vip_new);
        iv_dialog_backpay_cancel = (TextView) findViewById(R.id.iv_dialog_backpay_cancel);
        tv_dialog_backpay.setOnClickListener(listener);
        iv_dialog_backpay_cancel.setOnClickListener(listener);
        if (UserManager.getInstance().getNewFlag()) {
            iv_vip_new.setVisibility(View.VISIBLE);
        } else {
            iv_vip_new.setVisibility(View.GONE);
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                YoYo.with(Techniques.Pulse).duration(500).repeat(-1).playOn(tv_vip_next);
            }
        }, 300);
    }

    public void setContent(double price) {
        tv_vip_next.setText(price + "元试用");
    }
}
