package com.wdkl.app.ncs.callingbed.helper;

import android.app.Activity;
import android.app.AlertDialog;
import android.text.TextUtils;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.wdkl.app.ncs.callingbed.R;
import com.wdkl.app.ncs.callingbed.adapter.NumAdapter;
import com.wdkl.ncs.android.lib.base.BaseApplication;
import com.wdkl.ncs.android.lib.utils.ExtendMethodsKt;
import com.wdkl.ncs.android.middleware.utils.CommonUtils;


public class ServerConfigDialogHelper {

    private static AlertDialog callDialog;
    private static String pwd = "";

    public static void showPasswordDialog(final Activity activity) {
        View contentView = LayoutInflater.from(activity).inflate(R.layout.server_config_dialog_lay, null);
        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setView(contentView);

        final String[] numbers = {"1","2","3","4","5","6","7","8","9"};
        final TextView password = contentView.findViewById(R.id.tv_psw_view);
        final LinearLayout llPwd = contentView.findViewById(R.id.ll_password);
        final LinearLayout llServer = contentView.findViewById(R.id.ll_server_config);
        GridView gridView = contentView.findViewById(R.id.grid_psw);
        NumAdapter adapter = new NumAdapter(numbers, activity);
        gridView.setAdapter(adapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (pwd.length() <= 2) {
                    pwd = pwd + numbers[position];
                    password.setText(pwd);
                }
                Log.d("serverIp", "input password len: " + pwd.length() + "--" + pwd);
            }
        });

        TextView delete = contentView.findViewById(R.id.btn_delete);
        TextView cancel = contentView.findViewById(R.id.btn_cancel);
        TextView confirm = contentView.findViewById(R.id.btn_confirm);
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d("serverIp", "delete password len: " + pwd.length() + "--" + pwd);
                if (pwd.length() > 1) {
                    pwd = pwd.substring(0, pwd.length()-1);
                    password.setText(pwd);
                } else {
                    pwd = "";
                    password.setText(pwd);
                    password.setHint(R.string.input_password);
                }
            }
        });

        cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissCallDialog();
            }
        });

        confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if ("666".equals(pwd)) {
                    llPwd.setVisibility(View.GONE);
                    llServer.setVisibility(View.VISIBLE);
                } else {
                    Toast.makeText(activity, R.string.invalid_password, Toast.LENGTH_SHORT).show();
                }
            }
        });

        final EditText editUrl = contentView.findViewById(R.id.edit_url);
        final EditText editPort = contentView.findViewById(R.id.edit_port);
        final EditText editSipUrl = contentView.findViewById(R.id.edit_sip_url);
        final EditText editSipPort = contentView.findViewById(R.id.edit_sip_port);
        TextView saveConfig = contentView.findViewById(R.id.btn_save_config);
        TextView cancelConfig = contentView.findViewById(R.id.btn_cancel_config);
        editUrl.setText(CommonUtils.getUrl(BaseApplication.appContext));
        editPort.setText(CommonUtils.getUrlPort(BaseApplication.appContext));
        //editSipUrl.setText(CommonUtils.getSipUrl(BaseApplication.appContext));
        //editSipPort.setText(CommonUtils.getSipPort(BaseApplication.appContext));
        saveConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String url = editUrl.getText().toString();
                String port = editPort.getText().toString();
                if (TextUtils.isEmpty(url) || TextUtils.isEmpty(port)) {
                    ExtendMethodsKt.showMessage(R.string.input_empty);
                } else {
                    //保存配置
                    CommonUtils.setUrl(BaseApplication.appContext, editUrl.getText().toString());
                    CommonUtils.setUrlPort(BaseApplication.appContext, editPort.getText().toString());
                    //CommonUtils.setSipUrl(BaseApplication.appContext, editSipUrl.getText().toString());
                    //CommonUtils.setSipPort(BaseApplication.appContext, editSipPort.getText().toString());
                    dismissCallDialog();
                }
            }
        });
        cancelConfig.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismissCallDialog();
            }
        });


        callDialog = builder.create();
        callDialog.setCanceledOnTouchOutside(false);
        callDialog.setCancelable(false);
        callDialog.show();

        //设置dialog宽高及位置
        try {
            Window window = callDialog.getWindow();
            window.setFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE, WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
            WindowManager.LayoutParams lp = window.getAttributes();
            lp.width = 400;
            lp.height = 480;
            lp.gravity = Gravity.CENTER;
            window.setAttributes(lp);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_FULLSCREEN);
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void dismissCallDialog() {
        pwd = "";
        if (callDialog != null && callDialog.isShowing()) {
            callDialog.dismiss();
        }
    }
}
