/*
Copyright 2013 Andrew Joiner

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
 */

package uk.co.tekkies.readings.fragment;

import uk.co.tekkies.readings.R;
import uk.co.tekkies.readings.activity.PassageActivity;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Html;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.view.ScaleGestureDetector.SimpleOnScaleGestureListener;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ScrollView;
import android.widget.TextView;

public class PassageFragment extends Fragment {

    TextView textView;
    ScaleGestureDetector scaleGestureDetector;
    float defaultTextSize;
   
    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        Bundle args = getArguments();
        String passage = args.getString("passage");
        TextView textView1 = (TextView) (view.findViewById(R.id.textView1));
        String html = render(getPassageXml(passage));
        textView1.setText(Html.fromHtml(html));
        super.onViewCreated(view, savedInstanceState);
    }

    protected String render(String html) {
        html = html.replace("<summary>", "<i><font color=\"blue\">");
        html = html.replace("</summary>", "</font></i>");
        html = html.replace("<v>", "<sup><font color=\"red\">");
        html = html.replace("</v>", "</font></sup>");
        return html;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

    @Override
    public View onCreateView(android.view.LayoutInflater inflater, android.view.ViewGroup container,
            Bundle savedInstanceState) {
        View mainView = inflater.inflate(R.layout.passage_fragment, container, false);
        textView  = (TextView)mainView.findViewById(R.id.textView1);
        defaultTextSize = textView.getTextSize();
        scaleGestureDetector = new ScaleGestureDetector(getActivity(), new TextViewOnScaleGestureListener());
        ScrollView rootView = (ScrollView)mainView.findViewById(R.id.scrollView1);
        rootView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                scaleGestureDetector.onTouchEvent(event);
                return scaleGestureDetector.isInProgress();
            }
        });
        return mainView;
    }

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

    protected String getPassageXml(String passage) {
        String passageXml = "Error";
        String[] row = new String[] { "passage" };
        Cursor cursor = getActivity().getContentResolver().query(
                Uri.parse("content://uk.co.tekkies.plugin.bible.kjv/passage/" + passage), 
                row, 
                "", 
                row,
                "");
        if (cursor.moveToFirst()) {
            passageXml = cursor.getString(cursor.getColumnIndex("passage"));
        }
        return passageXml;
    }
  
    public class TextViewOnScaleGestureListener extends
    SimpleOnScaleGestureListener {
        
    double textScale = 1.0;
 
   @Override
   public boolean onScale(ScaleGestureDetector detector) {
       Log.v("ZOOM", "scaleFactor="+detector.getScaleFactor());
       textScale *= detector.getScaleFactor();
       Log.v("ZOOM", "scale="+textScale);
       textView.setTextSize((float) (textScale * defaultTextSize));
    return true;
   }

   @Override
   public boolean onScaleBegin(ScaleGestureDetector detector) {
       ((PassageActivity)getActivity()).requestViewPagerDisallowInterceptTouchEvent(true);
       return true;
   }

   @Override
   public void onScaleEnd(ScaleGestureDetector detector) {
       //((PassageActivity)getActivity()).viewPager.requestDisallowInterceptTouchEvent(false);
   }

  }
    
    
    
    
}
@@ -686,16 +686,58 @@
 tivity;%0A
+import android.content.SharedPreferences;%0A
 import a
@@ -756,24 +756,24 @@
 ase.Cursor;%0A
-
 import andro
@@ -810,16 +810,61 @@
 Bundle;%0A
+import android.preference.PreferenceManager;%0A
 import a
@@ -1336,16 +1336,92 @@
 ment %7B%0A%0A
+    private static final String PREF_PASSAGE_TEXT_SIZE = %22passageTextSize%22;%0A
     Text
@@ -1509,16 +1509,38 @@
 xtSize;%0A
+    double textSize;%0A%0A
    %0A    
@@ -1713,85 +1713,8 @@
 %22);%0A
-        TextView textView1 = (TextView) (view.findViewById(R.id.textView1));%0A
     
@@ -1779,17 +1779,16 @@
 textView
-1
 .setText
@@ -1864,32 +1864,603 @@
 eState);%0A    %7D%0A%0A
+    private void loadTextSize() %7B%0A        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());%0A        textSize= settings.getFloat(PREF_PASSAGE_TEXT_SIZE, 1);%0A        textView.setTextSize((float) (textSize * defaultTextSize));%0A    %7D%0A%0A    private void saveTextSize() %7B%0A        SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(getActivity());%0A        SharedPreferences.Editor editor = settings.edit();%0A        editor.putFloat(PREF_PASSAGE_TEXT_SIZE, (float) textSize);%0A        editor.commit();%0A    %7D%0A        %0A
     protected St
@@ -3173,17 +3173,16 @@
 extView 
- 
 = (TextV
@@ -3181,24 +3181,26 @@
 = (TextView)
+ (
 mainView.fin
@@ -3220,24 +3220,25 @@
 d.textView1)
+)
 ;%0A        de
@@ -3273,24 +3273,48 @@
 TextSize();%0A
+        loadTextSize();%0A
         scal
@@ -4664,36 +4664,8 @@
    %0A
-    double textScale = 1.0;%0A
  %0A  
@@ -4738,72 +4738,8 @@
 ) %7B%0A
-       Log.v(%22ZOOM%22, %22scaleFactor=%22+detector.getScaleFactor());%0A
     
@@ -4746,19 +4746,18 @@
    textS
-cal
+iz
 e *= det
@@ -4784,50 +4784,8 @@
 ();%0A
-       Log.v(%22ZOOM%22, %22scale=%22+textScale);%0A
     
@@ -4822,19 +4822,18 @@
 ) (textS
-cal
+iz
 e * defa
@@ -5147,93 +5147,21 @@
     
-//((PassageActivity)getActivity()).viewPager.requestDisallowInterceptTouchEvent(false
+saveTextSize(
 );%0A 

Save and load the text size in ShardPreferences