package xyz.jorgesanz.yamba;

import android.app.Fragment;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.conf.ConfigurationBuilder;

public class StatusFragment
        extends Fragment
        implements View.OnClickListener, TextWatcher {

    private static final String TAG = "StatusActivity";
    EditText statusEditText;
    Button tweetButton;
    Twitter twitter;
    TextView charsCounterTextView;
    ProgressBar tweetSendingProgressBar;
    ImageView twitterLogoImageView;

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

        statusEditText = view.findViewById(R.id.status_edit_text);
        tweetButton = view.findViewById(R.id.tweet_button);
        tweetButton.setOnClickListener(this);
        charsCounterTextView = view.findViewById(R.id.chars_counter_text_view);
        charsCounterTextView.setText(Integer.toString(140));
        charsCounterTextView.setTextColor(Color.GREEN);
        statusEditText.addTextChangedListener(this);
        tweetSendingProgressBar = view.findViewById(R.id.tweet_sending_progress_bar);
        tweetSendingProgressBar.setVisibility(View.GONE);
        twitterLogoImageView = view.findViewById(R.id.twitter_logo_image_view);
        twitterLogoImageView.setVisibility(View.VISIBLE);

        ConfigurationBuilder configurationBuilder = new ConfigurationBuilder();
        configurationBuilder.setOAuthConsumerKey(getString(R.string.oauth_consumer_key))
                .setOAuthConsumerSecret(getString(R.string.oauth_consumer_secret))
                .setOAuthAccessToken(getString(R.string.oauth_access_token))
                .setOAuthAccessTokenSecret(getString(R.string.oauth_access_token_secret));
        TwitterFactory twitterFactory = new TwitterFactory(configurationBuilder.build());
        twitter = twitterFactory.getInstance();

        return view;
    }

    public void onClick(View view) {
        String status = statusEditText.getText().toString();
        Log.d(TAG, "onClicked");

        new PostTask().execute(status);
    }

    @Override
    public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

    }

    @Override
    public void afterTextChanged(Editable statusEditText) {
        int count = 140 - statusEditText.length();
        charsCounterTextView.setText(Integer.toString(count));
        charsCounterTextView.setTextColor(Color.GREEN);
        if (count < 10) charsCounterTextView.setTextColor(Color.YELLOW);
        if (count < 0) charsCounterTextView.setTextColor(Color.RED);
    }

    private final class PostTask extends AsyncTask<String, Integer, SendingStatus> {

        @Override
        protected SendingStatus doInBackground(String... params) {

            try {
                twitter.updateStatus(params[0]);
                return SendingStatus.SUCCESSFUL;
            } catch (TwitterException e) {
                Log.e(TAG, "Sending failure");
                e.printStackTrace();
                if (e.getErrorCode() == -1) return SendingStatus.NETWORK_FAILED;
                return SendingStatus.TOKEN_FAILED;
            }
        }

        @Override
        protected void onPostExecute(SendingStatus result) {
            super.onPostExecute(result);

            tweetSendingProgressBar.setVisibility(View.GONE);
            twitterLogoImageView.setVisibility(View.VISIBLE);

            if (result == SendingStatus.SUCCESSFUL) {
                Snackbar.make(StatusFragment.this.getView(),
                        R.string.tweet_sent_snackbar_text,
                        Snackbar.LENGTH_LONG)
                        .show();
            } else if (result == SendingStatus.TOKEN_FAILED) {
                Snackbar.make(StatusFragment.this.getView(),
                        R.string.tweet_sending_token_failure_snackbar_text,
                        Snackbar.LENGTH_LONG)
                        .show();
            } else {
                Snackbar.make(StatusFragment.this.getView(),
                        R.string.tweet_sending_network_failure_snackbar_text,
                        Snackbar.LENGTH_LONG)
                        .show();
            }
        }

        @Override
        protected void onProgressUpdate(Integer... progress) {
            super.onProgressUpdate(progress);

            tweetSendingProgressBar.setProgress(progress[0]);
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            twitterLogoImageView.setVisibility(View.GONE);
            tweetSendingProgressBar.setVisibility(View.VISIBLE);
        }
    }
}

@@ -4192,26 +4192,30 @@
         
-if
+switch
  (result
  == Send
@@ -4210,36 +4210,8 @@
 sult
- == SendingStatus.SUCCESSFUL
 ) %7B%0A
@@ -4217,32 +4217,49 @@
 %0A               
+ case SUCCESSFUL:
  Snackbar.make(S
@@ -4442,74 +4442,57 @@
     
-%7D else if (result == SendingStatus.TOKEN_FAILED) %7B%0A               
+        break;%0A                case TOKEN_FAILED:
  Sna
@@ -4704,17 +4704,24 @@
     
-%7D else %7B%0A
+        break;%0A 
     
@@ -4723,32 +4723,40 @@
                 
+default:
  Snackbar.make(S
@@ -4901,32 +4901,32 @@
 ar.LENGTH_LONG)%0A
-
                 
@@ -4934,32 +4934,59 @@
        .show();%0A
+                    break;%0A
             %7D%0A  

Use switch statement for Enums treatment