package lt.vilnius.tvarkau.fragments;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.telephony.PhoneNumberFormattingTextWatcher;
import android.util.Patterns;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

import java.util.regex.Pattern;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnEditorAction;
import butterknife.OnTextChanged;
import lt.vilnius.tvarkau.R;
import lt.vilnius.tvarkau.entity.Profile;
import lt.vilnius.tvarkau.utils.SharedPrefsManager;

import static android.app.Activity.RESULT_OK;
import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static butterknife.OnTextChanged.Callback.AFTER_TEXT_CHANGED;


public class MyProfileFragment extends Fragment {

    private SharedPrefsManager prefsManager;

    @Bind(R.id.profile_name)
    EditText mProfileName;

    @Bind(R.id.profile_email)
    EditText mProfileEmail;

    @Bind(R.id.profile_telephone)
    EditText mProfileTelephone;


    public MyProfileFragment() {}

    public static MyProfileFragment getInstance() {
        return new MyProfileFragment();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.my_profile, container, false);

        ButterKnife.bind(this, view);

        return view;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setHasOptionsMenu(true);
        prefsManager = SharedPrefsManager.getInstance(getContext());
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        setUpUserProfile();
        mProfileTelephone.addTextChangedListener(new PhoneNumberFormattingTextWatcher());
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.my_profile_menu, menu);

        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                getActivity().onBackPressed();
                return true;
            case R.id.profile_submit:
                verifyAndSaveProfile();
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void verifyAndSaveProfile() {
        if (verifyAllFields()) {
            saveUserProfile();
        }
    }

    public void saveUserProfile() {
        String name = mProfileName.getText().toString();
        String email = mProfileEmail.getText().toString();
        String phone = mProfileTelephone.getText().toString();

        Profile profile = new Profile(name, email, phone);

        prefsManager.saveUserDetails(profile);

        getActivity().setResult(RESULT_OK);

        Toast.makeText(getContext(), "User profile saved. " +
                "Implement sending logic.", Toast.LENGTH_SHORT).show();
    }

    public boolean verifyAllFields() {
        verifyProfileName();
        verifyProfileEmail();
        verifyProfileTelephone();

        return mProfileName.getError() == null &&
                mProfileEmail.getError() == null &&
                mProfileTelephone.getError() == null;
    }

    private void setUpUserProfile() {
        if (!prefsManager.isUserAnonymous()) {
            Profile profile = Profile.returnProfile(getContext());

            mProfileName.setText(profile.getName());
            mProfileEmail.setText(profile.getEmail());
            mProfileTelephone.setText(profile.getMobilePhone());
        }
    }

    @OnTextChanged(value = R.id.profile_name, callback = AFTER_TEXT_CHANGED)
    public void verifyProfileName() {
        if (mProfileName.getText().length() == 0) {
            mProfileName.setError(getString(R.string.error_empty_field));
        } else {
            mProfileName.setError(null);
        }
    }

    @OnTextChanged(value = R.id.profile_email, callback = AFTER_TEXT_CHANGED)
    public void verifyProfileEmail() {
        Pattern pattern = Patterns.EMAIL_ADDRESS;

        if (mProfileEmail.getText().length() == 0) {
            mProfileEmail.setError(getString(R.string.error_empty_field));
        } else if (!pattern.matcher(mProfileEmail.getText().toString()).matches()) {
            mProfileEmail.setError(getString(R.string.error_email_incorrect));
        } else {
            mProfileEmail.setError(null);
        }
    }

    @OnEditorAction(R.id.profile_telephone)
    public boolean onTelephoneEditorAction(int actionId, KeyEvent event) {
        if (actionId == IME_ACTION_DONE) {
            verifyAndSaveProfile();

            return true;
        }

        return false;
    }

    public void verifyProfileTelephone() {
        Pattern pattern = Patterns.PHONE;

        if (mProfileTelephone.getText().length() == 0) {
            mProfileTelephone.setError(getString(R.string.error_empty_field));
        } else if (!pattern.matcher(mProfileTelephone.getText().toString()).matches()) {
            mProfileTelephone.setError(getString(R.string.error_phone_number_incorrect));
        } else {
            mProfileTelephone.setError(null);
        }
    }

    public boolean isEditedByUser() {
        if (mProfileName == null || mProfileEmail == null || mProfileTelephone == null) {
            return true;
        }

        String name = mProfileName.getText().toString();
        String email = mProfileEmail.getText().toString();
        String telephone = mProfileTelephone.getText().toString();

        if (name.length() == 0 && email.length() == 0 && telephone.length() == 0) {
            return true;
        }

        if (!prefsManager.isUserAnonymous()) {
            Profile oldProfile = prefsManager.getUserProfile();

            Profile newProfile = new Profile(name, email, telephone);

            return newProfile.equals(oldProfile);
        }

        return false;
    }

}

@@ -205,68 +205,8 @@
 er;%0A
-import android.util.Patterns;%0Aimport android.view.KeyEvent;%0A
 impo
@@ -450,41 +450,8 @@
 t;%0A%0A
-import java.util.regex.Pattern;%0A%0A
 impo
@@ -507,77 +507,8 @@
 fe;%0A
-import butterknife.OnEditorAction;%0Aimport butterknife.OnTextChanged;%0A
 impo
@@ -677,144 +677,8 @@
 OK;%0A
-import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;%0Aimport static butterknife.OnTextChanged.Callback.AFTER_TEXT_CHANGED;%0A
 %0A%0Apu
@@ -988,16 +988,21 @@
 ment() %7B
+%0A    
 %7D%0A%0A    p
@@ -2361,37 +2361,32 @@
             
-verifyAndSave
+saveUser
 Profile();%0A 
@@ -2513,130 +2513,8 @@
  %7D%0A%0A
-    public void verifyAndSaveProfile() %7B%0A        if (verifyAllFields()) %7B%0A            saveUserProfile();%0A        %7D%0A    %7D%0A%0A
     
@@ -3023,304 +3023,8 @@
  %7D%0A%0A
-    public boolean verifyAllFields() %7B%0A        verifyProfileName();%0A        verifyProfileEmail();%0A        verifyProfileTelephone();%0A%0A        return mProfileName.getError() == null &&%0A                mProfileEmail.getError() == null &&%0A                mProfileTelephone.getError() == null;%0A    %7D%0A%0A
     
@@ -3366,1605 +3366,8 @@
  %7D%0A%0A
-    @OnTextChanged(value = R.id.profile_name, callback = AFTER_TEXT_CHANGED)%0A    public void verifyProfileName() %7B%0A        if (mProfileName.getText().length() == 0) %7B%0A            mProfileName.setError(getString(R.string.error_empty_field));%0A        %7D else %7B%0A            mProfileName.setError(null);%0A        %7D%0A    %7D%0A%0A    @OnTextChanged(value = R.id.profile_email, callback = AFTER_TEXT_CHANGED)%0A    public void verifyProfileEmail() %7B%0A        Pattern pattern = Patterns.EMAIL_ADDRESS;%0A%0A        if (mProfileEmail.getText().length() == 0) %7B%0A            mProfileEmail.setError(getString(R.string.error_empty_field));%0A        %7D else if (!pattern.matcher(mProfileEmail.getText().toString()).matches()) %7B%0A            mProfileEmail.setError(getString(R.string.error_email_incorrect));%0A        %7D else %7B%0A            mProfileEmail.setError(null);%0A        %7D%0A    %7D%0A%0A    @OnEditorAction(R.id.profile_telephone)%0A    public boolean onTelephoneEditorAction(int actionId, KeyEvent event) %7B%0A        if (actionId == IME_ACTION_DONE) %7B%0A            verifyAndSaveProfile();%0A%0A            return true;%0A        %7D%0A%0A        return false;%0A    %7D%0A%0A    public void verifyProfileTelephone() %7B%0A        Pattern pattern = Patterns.PHONE;%0A%0A        if (mProfileTelephone.getText().length() == 0) %7B%0A            mProfileTelephone.setError(getString(R.string.error_empty_field));%0A        %7D else if (!pattern.matcher(mProfileTelephone.getText().toString()).matches()) %7B%0A            mProfileTelephone.setError(getString(R.string.error_phone_number_incorrect));%0A        %7D else %7B%0A            mProfileTelephone.setError(null);%0A        %7D%0A    %7D%0A%0A
     

Remove validation