package com.yidianling.ydlcommon.custom.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.yidianling.ydlcommon.R;
import io.reactivex.Observable;
import io.reactivex.disposables.Disposable;

import java.util.concurrent.TimeUnit;


/**
 * 通用dialog
 * a,支持单张图片显示
 * b,支持单双按钮显示
 * c,支持title
 * d,支持message
 * Created by yuawai on 2016/12/9.
 */
public class ChatTipsDialog extends Dialog {

    //是否可以点击对话框外部
    private Context context; //上下文对象
    private Disposable dissMissTimer;

    public static ChatTipsDialog create(Context context){
        return new ChatTipsDialog(context);
    }

    public ChatTipsDialog(Context context) {
        super(context, R.style.chatdialog_style);
        this.context = context;

        View v = LayoutInflater.from(context).inflate(R.layout.dialog_chat_tips,null);
        getWindow().setGravity(Gravity.TOP);
        setContentView(v,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        init();
//        setCanceledOnTouchOutside(false);
    }

    public ChatTipsDialog(Context context, int theme) {
        super(context, R.style.normaldialog_style);
        this.context = context;
    }

    private void init(){
        dissMissTimer = Observable.timer(5, TimeUnit.SECONDS).subscribe(aLong -> dissmissDialog());
    }

    private void dissmissDialog(){
        if (null != dissMissTimer){
            dissMissTimer.dispose();
        }
        dismiss();
    }

    @Override
    public void show() {
        if (null == context){
            return;
        }
        if (context instanceof Activity){
            if (((Activity)context).isFinishing()){
                return;
            }
        }
        super.show();
    }
}
