package org.shokai.voicetweet;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.androidannotations.annotations.Background;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.OptionsItem;
import org.androidannotations.annotations.OptionsMenu;
import org.androidannotations.annotations.OptionsMenuItem;
import org.androidannotations.annotations.UiThread;
import org.androidannotations.annotations.ViewById;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.User;

@EActivity(R.layout.activity_main)
@OptionsMenu(R.menu.menu_main)
public class MainActivity extends Activity {

    private final static String TAG = "MainActivity";
    public final static int CODE_TWITTER_LOGIN = 1;

    private TwitterUtil mTwitterUtil;
    private Twitter mTwitter;

    @ViewById(R.id.imageViewProfile)
    ImageView mImageViewProfile;

    @ViewById(R.id.textViewScreenName)
    TextView mTextViewScreenName;

    @OptionsMenuItem(R.id.action_tweet_test)
    MenuItem mItemTweetTest;

    @OptionsMenuItem(R.id.action_login)
    MenuItem mItemLogin;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mTwitterUtil = new TwitterUtil(this);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if(mTwitterUtil.hasToken()){
            mTwitter = mTwitterUtil.getTwitterInstance();
            getTwitterScreenNameAsync();
        }
        else{
            mTextViewScreenName.setText(getResources().getText(R.string.text_screen_name));
            mImageViewProfile.setVisibility(View.INVISIBLE);
        }
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        mItemTweetTest.setVisible(BuildConfig.DEBUG && mTwitterUtil.hasToken());
        mItemLogin.setTitle(mTwitterUtil.hasToken() ? "Logout" : "Login");
        return super.onPrepareOptionsMenu(menu);
    }

    @OptionsItem(R.id.action_login)
    void login(){
        TwitterOAuthActivity_.intent(this).startForResult(CODE_TWITTER_LOGIN);
    }

    @OptionsItem(R.id.action_tweet_test)
    void tweetTest(){
        TweetTestActivity_.intent(this).start();
    }

    @Background
    void getTwitterScreenNameAsync(){
        if(mTwitter == null) return;
        try {
            String name = mTwitter.getScreenName();
            displayTwitterScreenName(name);
            getTwitterProfileImageAsync(name);
        } catch (TwitterException e) {
            e.printStackTrace();
        }
    }

    @UiThread
    void displayTwitterScreenName(String name){
        Log.i(TAG, "screen_name: " + name);
        mTextViewScreenName.setText("@" + name);
    }

    @Background
    void getTwitterProfileImageAsync(String screenName) {
        try {
            User user = mTwitter.showUser(screenName);
            URL imageUrl = new URL(user.getBiggerProfileImageURL());
            Bitmap profileImage = BitmapFactory.decodeStream(imageUrl.openConnection().getInputStream());
            displayTwitterProfileImage(profileImage);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (TwitterException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @UiThread
    void displayTwitterProfileImage(Bitmap image){
        Log.i(TAG, "load profile image");
        mImageViewProfile.setImageBitmap(image);
        mImageViewProfile.setVisibility(View.VISIBLE);
    }

}

@@ -1165,16 +1165,55 @@
 Twitter;
+%0A    private String mScreenName = null;
 %0A%0A    @V
@@ -1864,24 +1864,67 @@
 ameAsync();%0A
+            getTwitterProfileImageAsync();%0A
         %7D%0A  
@@ -2650,32 +2650,52 @@
 %0A    @Background
+(serial = %22twitter%22)
 %0A    void getTwi
@@ -2780,24 +2780,24 @@
         
-String n
+mScreenN
 ame = mT
@@ -2832,30 +2832,47 @@
         
-displayTwitter
+Log.i(TAG, %22screen_name: @%22 + m
 ScreenNa
@@ -2873,21 +2873,16 @@
 reenName
-(name
 );%0A     
@@ -2892,43 +2892,48 @@
     
-getTwitterProfileImageAsync(n
+displayTwitterScreenName(mScreenN
 ame);%0A
+%0A
     
@@ -3083,52 +3083,8 @@
 e)%7B%0A
-        Log.i(TAG, %22screen_name: %22 + name);%0A
     
@@ -3150,16 +3150,36 @@
 ckground
+(serial = %22twitter%22)
 %0A    voi
@@ -3212,28 +3212,104 @@
 ync(
-String screenName) %7B
+) %7B%0A        Log.i(TAG, %22getTWitterProfileImageAsync()%22);%0A        if(mScreenName == null) return;
 %0A   
@@ -3361,17 +3361,18 @@
 howUser(
-s
+mS
 creenNam
@@ -3546,24 +3546,70 @@
 tStream());%0A
+            Log.i(TAG, %22load profile image%22);%0A
             
@@ -3952,50 +3952,8 @@
 e)%7B%0A
-        Log.i(TAG, %22load profile image%22);%0A
     

use "serial" property of @Background to get screen name then get profile image #16