package com.sheep.gamegroup.view.dialog;

import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.TextView;

import com.sheep.gamegroup.absBase.AbsObserver;
import com.sheep.gamegroup.util.ActivityManager;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * Created by realicing on 2018/11/7.
 * realicing@sina.com
 */
public class DialogSuccessUtil {
    public static void showSuccess(String msg, DialogInterface.OnDismissListener onDismissListener) {
        View dialog_parent = View.inflate(SheepApp.getInstance(), R.layout.dialog_success, null);
        final AlertDialog dialog = new AlertDialog.Builder(ActivityManager.getInstance().currentActivity(), R.style.AppTheme_Dialog_Alert)
                .setView(dialog_parent)
                .create();
        TextView dialog_title = dialog_parent.findViewById(R.id.dialog_title);
        final View close = dialog_parent.findViewById(R.id.dialog_close);
        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    dialog.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
        dialog_title.setText(msg);
        dialog.setOnDismissListener(onDismissListener);
        try {
            dialog.show();
        } catch (Exception e) {
            e.printStackTrace();
        }

        //3秒后自动消失
        Observable.just(1).delay(3, TimeUnit.SECONDS)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new AbsObserver<Integer>() {
                    @Override
                    public void onNext(Integer integer) {
                        try {
                            if(dialog.isShowing())
                                close.performClick();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
