/*
The MIT License (MIT)

Copyright (c) 2013, V. Giacometti, M. Giuriato, B. Petrantuono

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

package com.example.manan.enhancedurdureader.EpubReader;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.example.manan.enhancedurdureader.R;

//Panel specialized in visualizing HTML-data
public class DataView extends SplitPanel {
	protected WebView view;
	protected String data;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)	{
		super.onCreateView(inflater, container, savedInstanceState);
		View v = inflater.inflate(R.layout.activity_data_view, container, false);
		return v;
	}
	
	@Override
    public void onActivityCreated(Bundle saved) {
		super.onActivityCreated(saved);
		view = (WebView) getView().findViewById(R.id.Viewport);
		
		view.setWebViewClient(new WebViewClient() {
			public boolean shouldOverrideUrlLoading(WebView view, String url) {
				try {
					navigator.setBookPage(url, index);
				} catch (Exception e) {
					errorMessage(getString(R.string.error_LoadPage));
				}
				return true;
			}
		});
		
		loadData(data);
	}
	
	public void loadData(String source)
	{
		/*data = "<head>\n" +
				"<style type=\"text/css\"> \n" +
				"@font-face { font-family: nastaleeq;  \n" +
				"src:url(\"~/android_asset/fonts/nastaleeq.ttf\") } \n" +
				"h2{ font-family: MyCustomFont; \n" +
				"font-size:18; \n" +
				"text-align: center;\n" +
				"line-height: normal;\n" +
				"margin:auto;\n" +
				"} \n" +
				"</style>\n" +
				"</head><body>";
		data+=source;
		data +="</body>";*/
		data = source;
		
		if(created)
			view.loadData(data, getActivity().getApplicationContext().getResources().getString(R.string.textOrHTML), null);
	}

	@Override
	public void saveState(Editor editor)
	{
		super.saveState(editor);
		editor.putString("data"+index, data);
	}
	
	@Override
	public void loadState(SharedPreferences preferences)
	{
		super.loadState(preferences);
		loadData(preferences.getString("data"+index, ""));
	}
}
