package com.fisheradelakin.interactivestory.ui;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.fisheradelakin.interactivestory.R;
import com.fisheradelakin.interactivestory.model.Page;
import com.fisheradelakin.interactivestory.model.Story;


public class StoryActivity extends ActionBarActivity {

    public static final String TAG = StoryActivity.class.getSimpleName();

    private Story mStory = new Story();
    private ImageView mImageView;
    private TextView mTextView;
    private Button mChoice1;
    private Button mChoice2;
    private String mName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_story);

        Intent intent = getIntent();
        mName = intent.getStringExtra(getString(R.string.key_name));

        if(mName == null) {
            mName="Friend";
        }

        Log.d(TAG, mName);

        mImageView = (ImageView) findViewById(R.id.storyImageView);
        mTextView = (TextView) findViewById(R.id.storyTextView);
        mChoice1 = (Button) findViewById(R.id.choiceButton1);
        mChoice2 = (Button) findViewById(R.id.choiceButton2);

        loadPage();
    }

    private void loadPage() {
        Page page = mStory.getPage(0);

        Drawable drawable = getResources().getDrawable(page.getImageId());
        mImageView.setImageDrawable(drawable);

        String pageText = page.getText();
        // Add the name if placeholder is included. will not add if no placeholder
        pageText = String.format(pageText, mName);
        mTextView.setText(pageText);

        mChoice1.setText(page.getChoice1().getText());
        mChoice2.setText(page.getChoice2().getText());
    }
}

@@ -216,16 +216,42 @@
 il.Log;%0A
+import android.view.View;%0A
 import a
@@ -817,16 +817,47 @@
 g mName;
+%0A    private Page mCurrentPage;
 %0A%0A    @O
@@ -1492,16 +1492,17 @@
 oadPage(
+0
 );%0A    %7D
@@ -1529,16 +1529,26 @@
 oadPage(
+int choice
 ) %7B%0A    
@@ -1555,14 +1555,17 @@
     
-Page p
+mCurrentP
 age 
@@ -1581,17 +1581,22 @@
 getPage(
-0
+choice
 );%0A%0A    
@@ -1646,17 +1646,25 @@
 rawable(
-p
+mCurrentP
 age.getI
@@ -1748,17 +1748,25 @@
 eText = 
-p
+mCurrentP
 age.getT
@@ -1961,33 +1961,41 @@
 Choice1.setText(
-p
+mCurrentP
 age.getChoice1()
@@ -2036,35 +2036,571 @@
 ext(
-page.getChoice2().getText()
+mCurrentPage.getChoice2().getText());%0A%0A        mChoice1.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                int nextPage = mCurrentPage.getChoice1().getNextPage();%0A                loadPage(nextPage);%0A            %7D%0A        %7D);%0A%0A        mChoice2.setOnClickListener(new View.OnClickListener() %7B%0A            @Override%0A            public void onClick(View v) %7B%0A                int nextPage = mCurrentPage.getChoice2().getNextPage();%0A                loadPage(nextPage);%0A            %7D%0A        %7D
 );%0A 

load additional pages