package com.example.ismailamrani.comptable.ui.startup;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.ismailamrani.comptable.R;
import com.example.ismailamrani.comptable.ui.base.ColoredStatusBarActivity;
import com.example.ismailamrani.comptable.utils.Method;
import com.example.ismailamrani.comptable.utils.RequestListener;
import com.example.ismailamrani.comptable.webservice.PhpAPI;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;

public class ActivationActivity extends ColoredStatusBarActivity {

    @Bind(R.id.activationField)
    EditText activationField;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_activation);
        ButterKnife.bind(this);
    }

    public void onActivate(View view) {
        String serial = activationField.getText().toString();
        if (!TextUtils.isEmpty(serial)) {
            sendHTTPRequest(
                    PhpAPI.activateApp,
                    serialToJSON(serial),
                    Method.POST,
                    new ActivationListener()
            );
        } else
            Toast.makeText(this, "Activation code required.", Toast.LENGTH_LONG).show();
    }

    private JSONObject serialToJSON(String serial) {
        JSONObject data = new JSONObject();

        try {
            data.put("serial", serial);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return data;
    }

    /**
     * A listener on the activation procedure.
     */
    public class ActivationListener implements RequestListener {
        @Override
        public void onRequestSucceeded(JSONObject response, int status) {
            try {
                String message = response.getString("message");

                runOnUiThread(() -> {
                    if (status == 1) {
                        Toast.makeText(ActivationActivity.this,
                                message, Toast.LENGTH_SHORT).show();
                    } else if (status == -1) {
                        Toast.makeText(ActivationActivity.this,
                                message, Toast.LENGTH_SHORT).show();
                    } else if (status == 0) {
                        Toast.makeText(ActivationActivity.this,
                                message, Toast.LENGTH_SHORT).show();
                    }
                });
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }

        @Override
        public void onRequestFailed() {
            runOnUiThread(() -> Toast.makeText(ActivationActivity.this,
                    "Unknown error.", Toast.LENGTH_SHORT).show());
        }
    }
}

@@ -95,25 +95,88 @@
 id.text.
-TextUtils
+Editable;%0Aimport android.text.TextUtils;%0Aimport android.text.TextWatcher
 ;%0Aimport
@@ -617,24 +617,50 @@
 SONObject;%0A%0A
+import java.util.Arrays;%0A%0A
 import butte
@@ -1036,16 +1036,86 @@
 d(this);
+%0A%0A        activationField.addTextChangedListener(new SerialWatcher());
 %0A    %7D%0A%0A
@@ -2971,32 +2971,32 @@
 nActivity.this,%0A
-
                 
@@ -3061,11 +3061,1195 @@
  %7D%0A    %7D
+%0A%0A    /**%0A     * A text watcher on the activation text field.%0A     */%0A    public class SerialWatcher implements TextWatcher %7B%0A%0A        private boolean isUpdating;%0A%0A        @Override%0A        public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0A%0A        %7D%0A%0A        @Override%0A        public void onTextChanged(CharSequence s, int start, int before, int count) %7B%0A            if (isUpdating) %7B%0A                isUpdating = false;%0A                return;%0A            %7D%0A%0A            String serial = s.toString().replaceAll(%22(%5B%5E%5C%5Cda-zA-Z%5D)%22, %22%22);%0A            int position = 0;%0A            StringBuilder sb = new StringBuilder();%0A%0A            for (char c : serial.toCharArray()) %7B%0A                sb.append(Arrays.asList(4, 8, 12).contains(position) ? %22-%22 + c : c);%0A                position++;%0A            %7D%0A%0A            isUpdating = true;%0A            String result = sb.toString().toUpperCase();%0A            activationField.setText(result.length() %3E 19 ? result.substring(0, 19) : result);%0A            activationField.setSelection(activationField.getText().length());%0A        %7D%0A%0A        @Override%0A        public void afterTextChanged(Editable s) %7B%0A%0A        %7D%0A    %7D
 %0A%7D%0A

set a TextWatcher on the serial text field.