package com.yyrh.ui.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
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 com.yyrh.constant.SDKSettings;
import com.yyrh.networking.NetRequestApi;
import com.yyrh.sdk.YyrhSdkManager;
import com.yyrh.sdk.callback.LoginCallback;

import com.yyrh.utils.ResourceUtil;
import com.yyrh.utils.TosUtil;
import com.yyrh.utils.Utils;

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


public class AutoLoginDialog extends Dialog {


    private static TextView hnyy_auto_login_account_text, hnyy_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;

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

        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);

                hnyy_auto_login_cancel_text = dialog.findViewById(ResourceUtil.getId(context,
                        "hnyy_auto_login_cancel_text"));
                hnyy_auto_login_account_text = dialog.findViewById(ResourceUtil.getId(context,
                        "hnyy_auto_login_account_text"));


                if (hnyy_auto_login_cancel_text != null) {
                    hnyy_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"));
                String autoTip = Utils.getSharedPreferences("yyrh", "username", context) + hnyy_enter_game_tip;
                hnyy_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();

                SharedPreferences sp = context.getSharedPreferences("yyrh", Activity.MODE_PRIVATE);
                final String account = sp.getString("username", "");
                final String pwd = sp.getString("password", "");
                final String login_token = sp.getString("login_token", "");
                NetRequestApi.loginWithYy(false, (Activity) context, account, pwd, login_token, new LoginCallback() {
                    @Override
                    public void onLoginSuccess(String token) {

                        closeDialogCallback.onclose();

                        YyrhSdkManager.defaultManager().saveUserLoginInfo(context, account, pwd, login_token, token);

                    }

                    @Override
                    public void onLoginFailed(String msg) {
                        new TosUtil(context, TosUtil.TOAST_ERROR, msg).show();

                        closeDialogCallback.onLoginFail();
                    }

                    @Override
                    public void onLoginError(String msg) {
                        new TosUtil(context, TosUtil.TOAST_ERROR, msg).show();


                    }
                });
                Looper.loop();
            }

        };


    }

    public interface CloseDialogCallback {
        void onclose();

        void onLoginFail();
    }

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