package com.ericsson.uecontrol.gui;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.webkit.WebView;

import com.ericsson.uecontrol.BuildConfig;

import org.apache.log4j.Logger;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by ezivkoc on 2014-07-28.
 */
public class AboutActivity extends Activity{
    private static final Logger log = Logger.getLogger(AboutActivity.class);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        String html = "";
        try {
            html = getAssetContent("www/about.html");

            // Replace keywords
            html = html.replace("{VERSION}", BuildConfig.VERSION_NAME);
            html = html.replace("{BUILD}", "" + BuildConfig.VERSION_CODE);
        } catch(IOException e){
            html = e.getMessage();
            log.error(null, e);
        }

        WebView view = new WebView(this);
        //view.loadUrl("file:///android_asset/www/about.html");
        view.loadData(html, "text/html", "UTF-8");
        this.setContentView(view);
    }


    public String getAssetContent(String file) throws IOException {
        StringBuilder ret = new StringBuilder();
        AssetManager assetManager = getResources().getAssets();
        BufferedReader reader = new BufferedReader(
                new InputStreamReader(
                        assetManager.open(file)));
        String line = null;
        while ((line = reader.readLine()) != null) {
            ret.append(line).append("\n");
        }
        reader.close();
        return ret.toString();
    }
}
