package cn.yyxx.commsdk.impl.merge.platform.hnyy.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Looper;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

import cn.yyxx.commsdk.base.entity.ResultInfo;
import cn.yyxx.commsdk.base.internal.ICallback;
import cn.yyxx.commsdk.core.utils.ResourceUtil;
import cn.yyxx.commsdk.impl.merge.LoginViewModel;
import cn.yyxx.commsdk.impl.merge.user.UserSession;
import cn.yyxx.commsdk.impl.merge.user.UserSessionManager;


public class AutoLoginDialog extends Dialog {


    private static TextView auto_login_account_text, auto_login_cancel_text;
    private static AutoLoginDialog dialog;
    private static Timer timer;

    public AutoLoginDialog(Context context, int theme) {
        super(context, theme);

    }

    public static class Builder {
        private Context context;
        private OnClickListener positiveButtonClickListener;
        private CloseDialogCallback closeDialogCallback;

        private LoginViewModel loginViewModel;

        private UserSession lastUserSession;

        public Builder(Context context, LoginViewModel loginViewModel) {
            this.context = context;
            this.loginViewModel = loginViewModel;
        }

        public Builder setPositiveButton(OnClickListener listener) {
            this.positiveButtonClickListener = listener;
            return this;
        }

        public Builder setCloseDialogCallback(CloseDialogCallback closeDialogCallback) {
            this.closeDialogCallback = closeDialogCallback;
            return this;
        }


        public void dialogDismiss() {
            if (timer != null) {
                timer.cancel();
            }

            dialog.dismiss();
        }

        public AutoLoginDialog Create() {
            {
                LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                dialog = new AutoLoginDialog(context, ResourceUtil.getStyleId(context, "hnyy_auto_login_dialog"));
                View layout = inflater.inflate(ResourceUtil.getLayoutId(context, "hnyy_quick_login_bar"), null);
                ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                Window window = dialog.getWindow();
                window.setGravity(Gravity.TOP);
                WindowManager.LayoutParams lp = window.getAttributes();

                lp.y = dp2Px(50, context);  //设置出现的高度，距离顶部
                window.setAttributes(lp);

                dialog.addContentView(layout, layoutParams);

                auto_login_cancel_text = dialog.findViewById(ResourceUtil.getId(context,
                        "auto_login_cancel_text"));
                auto_login_account_text = dialog.findViewById(ResourceUtil.getId(context,
                        "auto_login_account_text"));


                if (auto_login_cancel_text != null) {
                    auto_login_cancel_text.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            if (timer != null) {
                                timer.cancel();
                            }

                            timer = null;
                            positiveButtonClickListener.onClick(dialog, DialogInterface.BUTTON_POSITIVE);
                        }
                    });
                }

                String hnyy_enter_game_tip = context.getString(ResourceUtil.getStringId(context, "hnyy_enter_game_tip"));
                lastUserSession = UserSessionManager.getInstance().getLocalLastSession();
                String autoTip = lastUserSession.uname + hnyy_enter_game_tip;

                auto_login_account_text.setText(autoTip);
                dialog.setCancelable(false);
                timer = new Timer();
                timer.schedule(task, 2000);
                return dialog;
            }
        }

        TimerTask task = new TimerTask() {

            @Override
            public void run() {
                Looper.prepare();

                dialogDismiss();

                loginViewModel.accountLogin((Activity) context, lastUserSession.uname, lastUserSession.password, lastUserSession.login_token, new ICallback() {
                    @Override
                    public void onResult(int code, String result) {
                        if (code == ResultInfo.CODE_SUCCESS){
                            closeDialogCallback.onLoginSuccess();
                        }else {
                            closeDialogCallback.onLoginFail();

                        }

                    }
                });

                Looper.loop();
            }

        };


    }

    public interface CloseDialogCallback {
        void onLoginSuccess();

        void onLoginFail();
    }

    public static int dp2Px(float dp, Context mContext) {
        return (int) TypedValue.applyDimension(
                TypedValue.COMPLEX_UNIT_DIP,
                dp,
                mContext.getResources().getDisplayMetrics());
    }
}
