package com.tacticalnuclearstrike.tttumblr.activites;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuItem.OnMenuItemClickListener;
import android.view.SubMenu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.util.Log;

import com.tacticalnuclearstrike.tttumblr.R;
import com.tacticalnuclearstrike.tttumblr.TumblrApi;

/** Base class for activities which post content.
 * provides the menu for setting post options.
 */
public abstract class PostActivity extends Activity {

    private static final String TAG = "PostActivity";

    //menu group for blog selection list.
    protected static final int MENU_GROUP_BLOG = 1;
    protected static final int MENU_GROUP_TWEET = 2;
    protected static final int MENU_GROUP_PRIVATE = 3;

    protected Bundle mPostOptions;
    protected SharedPreferences mBloglist;
    protected SharedPreferences mPrefs;

	@Override
	public void onCreate(Bundle savedInstanceState) {
        //set defaults for common post options.
        mPostOptions = TumblrApi.getDefaultPostOptions(this);
        mBloglist = getSharedPreferences(TumblrApi.BLOGS_PREFS, 0);
        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);
        loadDefaultPostOptions();
		super.onCreate(savedInstanceState);

	}
	
	private void returnToMainActivity() {
		setResult(RESULT_OK);
		finish();
	}

    protected void loadDefaultPostOptions(){
        //TODO: fill this in.
    }

    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        
        OnMenuItemClickListener blogchoice_listener = new OnMenuItemClickListener(){
            public boolean onMenuItemClick(MenuItem mi){
                Log.d(TAG, "setting tumblelog to " + mBloglist.getString(mi.getTitle().toString(), "unknown!!"));
                mPostOptions.putString("group", mBloglist.getString(mi.getTitle().toString(), "unknown"));
                return true;
            }
        };
                
        SubMenu blogmenu = menu.addSubMenu(Menu.NONE, Menu.NONE, Menu.NONE, "Select Tumblelog");
        blogmenu.setIcon(android.R.drawable.ic_menu_agenda);
        //create the sub-menu based on the stored list of preferences.
        for (String k : mBloglist.getAll().keySet()){
            MenuItem blogitem = blogmenu.add(MENU_GROUP_BLOG, Menu.NONE, Menu.NONE, k);
            blogitem.setOnMenuItemClickListener(blogchoice_listener);
            if(mPrefs.getString("default_blog", "NO DEFAULT").equals(mBloglist.getString(k,""))){
                //this is our default blog. mark it as selected.
                blogitem.setChecked(true);
            }
        }
        blogmenu.setGroupCheckable(MENU_GROUP_BLOG, true, true); 

        //add a submenu for twitter
        SubMenu tweetmenu = menu.addSubMenu(Menu.NONE, Menu.NONE, Menu.NONE, "Twitter");
        tweetmenu.setIcon(android.R.drawable.ic_menu_share);
        MenuItem tweet = tweetmenu.add(MENU_GROUP_TWEET, Menu.NONE, Menu.NONE, "send to twitter");
        tweet.setOnMenuItemClickListener(new OnMenuItemClickListener(){
            public boolean onMenuItemClick(MenuItem mi){
                mPostOptions.putString("send-to-twitter", "auto");
                return true;
            }
        });
        if( mPostOptions.getString("send-to-twitter").equals("auto"))
            tweet.setChecked(true);

        MenuItem notweet = tweetmenu.add(MENU_GROUP_TWEET, Menu.NONE, Menu.NONE, "do not send");
        notweet.setOnMenuItemClickListener(new OnMenuItemClickListener(){
            public boolean onMenuItemClick(MenuItem mi){
                mPostOptions.putString("send-to-twitter", "no");
                return true;
            }
        });
        if( mPostOptions.getString("send-to-twitter").equals("no"))
            notweet.setChecked(true);
        tweetmenu.setGroupCheckable(MENU_GROUP_TWEET, true, true); 

        //add a submenu for "private" posts.
        SubMenu privmenu = menu.addSubMenu(Menu.NONE, Menu.NONE, Menu.NONE, "Post Privately?");
        privmenu.setIcon(android.R.drawable.ic_menu_view);
        MenuItem mi = privmenu.add(MENU_GROUP_PRIVATE, Menu.NONE, Menu.NONE, "yes");
        mi.setOnMenuItemClickListener(new OnMenuItemClickListener(){
            public boolean onMenuItemClick(MenuItem mi){
                mPostOptions.putString("private", "1");
                return true;
            }
        });
        if( mPostOptions.getString("private").equals("1"))
            mi.setChecked(true);

        mi = privmenu.add(MENU_GROUP_PRIVATE, Menu.NONE, Menu.NONE, "no");
        mi.setOnMenuItemClickListener(new OnMenuItemClickListener(){
            public boolean onMenuItemClick(MenuItem mi){
                mPostOptions.putString("private", "0");
                return true;
            }
        });
        if( mPostOptions.getString("private").equals("0"))
            mi.setChecked(true);
        privmenu.setGroupCheckable(MENU_GROUP_PRIVATE, true, true); 

        return true;
    }
}

@@ -2144,24 +2144,61 @@
 unknown%22));%0A
+                mi.setChecked(true);%0A
             
@@ -2206,32 +2206,32 @@
    return true;%0A
-
             %7D%0A  
@@ -3471,24 +3471,61 @@
 %22, %22auto%22);%0A
+                mi.setChecked(true);%0A
             
@@ -3570,114 +3570,8 @@
  %7D);
-%0A        if( mPostOptions.getString(%22send-to-twitter%22).equals(%22auto%22))%0A            tweet.setChecked(true);
 %0A%0A  
@@ -3853,32 +3853,69 @@
 witter%22, %22no%22);%0A
+                mi.setChecked(true);%0A
                 
@@ -3953,32 +3953,182 @@
     %7D);%0A        
+//set default item checked.%0A        if( mPostOptions.getString(%22send-to-twitter%22).equals(%22auto%22))%7B%0A            tweet.setChecked(true);%0A        %7D else 
 if( mPostOptions
@@ -4170,16 +4170,17 @@
 s(%22no%22))
+%7B
 %0A       
@@ -4202,32 +4202,42 @@
 tChecked(true);%0A
+        %7D%0A
         tweetmen
@@ -4752,24 +4752,61 @@
 ate%22, %221%22);%0A
+                mi.setChecked(true);%0A
             
@@ -5134,32 +5134,32 @@
 k(MenuItem mi)%7B%0A
-
                 
@@ -5194,24 +5194,61 @@
 ate%22, %220%22);%0A
+                mi.setChecked(true);%0A
             

update menuitem state when changing postoptions