package be.xvrt.times.controller;

import com.parse.ParseException;
import com.parse.ParseUser;
import com.parse.SignUpCallback;

import android.app.FragmentManager;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.text.method.TransformationMethod;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;
import be.xvrt.times.R;
import be.xvrt.times.view.ShowClocksFragment;
import butterknife.InjectView;
import butterknife.OnCheckedChanged;
import butterknife.OnClick;

public final class RegisterFragmentController {

    private final FragmentManager fragmentManager;

    @InjectView(R.id.progressBar)
    ProgressBar progressBar;

    @InjectView(R.id.errorTxt)
    TextView errorView;

    @InjectView(R.id.emailTxt)
    TextView emailView;

    @InjectView(R.id.passwordTxt)
    TextView passwordView;

    @InjectView(R.id.registerBtn)
    TextView registerBtn;

    public RegisterFragmentController(FragmentManager fragmentManager) {
        this.fragmentManager = fragmentManager;
    }

    @OnCheckedChanged(R.id.showPasswordCbx)
    void toggleShowPassword() {
        TransformationMethod currentTransformationMethod = passwordView.getTransformationMethod();

        if (currentTransformationMethod instanceof HideReturnsTransformationMethod) {
            passwordView.setTransformationMethod(PasswordTransformationMethod.getInstance());
        } else {
            passwordView.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        }
    }

    @OnClick(R.id.registerBtn)
    void handleRegister() {
        registerBtn.setEnabled(false);
        emailView.setEnabled(false);
        passwordView.setEnabled(false);

        progressBar.setVisibility(View.VISIBLE);
        errorView.setVisibility(View.GONE);

        String email = emailView.getText().toString();
        String password = passwordView.getText().toString();

        if (email.length() == 0) {
            showError("user name cannot be empty");
        } else if (password.length() == 0) {
            showError("password cannot be empty");
        } else {
            ParseUser user = new ParseUser();
            user.setUsername(email);
            user.setPassword(password);
            user.setEmail(email);

            user.signUpInBackground(new SignUpCallback() {
                public void done(ParseException exception) {
                    if (exception == null) {
                        handleSuccess();
                    } else {
                        String message = exception.getMessage();
                        if (message.contains("HttpHostConnectException")) {
                            message = "no network connection available";
                        }

                        showError(message);
                    }
                }
            });
        }
    }

    private void showError(String message) {
        registerBtn.setEnabled(true);
        emailView.setEnabled(true);
        passwordView.setEnabled(true);

        progressBar.setVisibility(View.GONE);

        errorView.setText(message);
        errorView.setVisibility(View.VISIBLE);
    }

    private void handleSuccess() {
        fragmentManager.popBackStack();
        fragmentManager.beginTransaction()
                       .replace(R.id.main_fragment_container, new ShowClocksFragment(), ShowClocksFragment.TAG)
                       .commit();
    }

}

@@ -347,24 +347,56 @@
 .view.View;%0A
+import android.widget.CheckBox;%0A
 import andro
@@ -418,16 +418,16 @@
 essBar;%0A
-
 import a
@@ -952,24 +952,93 @@
 swordView;%0A%0A
+    @InjectView(R.id.showPasswordCbx)%0A    CheckBox showPasswordCbx;%0A%0A
     @InjectV
@@ -1875,16 +1875,59 @@
 (false);
+%0A        showPasswordCbx.setEnabled(false);
 %0A%0A      
@@ -3193,32 +3193,32 @@
 tEnabled(true);%0A
-
         password
@@ -3239,16 +3239,58 @@
 d(true);
+%0A        showPasswordCbx.setEnabled(true);
 %0A%0A      

Disable show password when register is running.