/*
 * Copyright (C) 2016 Pedro Paulo de Amorim
 *
 * 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 com.folioreader.android.sample;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectReader;
import com.folioreader.Config;
import com.folioreader.FolioReader;
import com.folioreader.model.HighLight;
import com.folioreader.model.ReadPosition;
import com.folioreader.model.ReadPositionImpl;
import com.folioreader.ui.base.OnSaveHighlight;
import com.folioreader.util.AppUtil;
import com.folioreader.util.ObjectMapperSingleton;
import com.folioreader.util.OnHighlightListener;
import com.folioreader.util.ReadPositionListener;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class HomeActivity extends AppCompatActivity
        implements OnHighlightListener, ReadPositionListener {

    private static final String LOG_TAG = HomeActivity.class.getSimpleName();
    private FolioReader folioReader;
    private Config config;

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

        config = AppUtil.getSavedConfig(getApplicationContext());
        if (config == null)
            config = new Config();
        config.setAllowedDirection(Config.AllowedDirection.VERTICAL_AND_HORIZONTAL);

        folioReader = FolioReader.get()
                .setOnHighlightListener(this)
                .setReadPositionListener(this);

        getHighlightsAndSave();

        findViewById(R.id.btn_raw).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                folioReader.setConfig(config, true)
                        .openBook(R.raw.adventures);
            }
        });

        findViewById(R.id.btn_assest).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                ReadPosition readPosition = getLastReadPosition();

                folioReader.setReadPosition(readPosition)
                        .setConfig(config, true)
                        .openBook("file:///android_asset/TheSilverChair.epub");
            }
        });
    }

    private ReadPosition getLastReadPosition() {

        ReadPosition readPosition = null;
        ObjectReader objectReader = ObjectMapperSingleton.getObjectMapper().reader();

        try {
            readPosition = objectReader.forType(ReadPositionImpl.class)
                    .readValue(getAssets().open("read_positions/read_position.json"));
        } catch (IOException e) {
            e.printStackTrace();
        }

        return readPosition;
    }

    @Override
    public void saveReadPosition(ReadPosition readPosition) {

        //Toast.makeText(this, "ReadPosition = " + readPosition.toJson(), Toast.LENGTH_SHORT).show();
        Log.i(LOG_TAG, "-> ReadPosition = " + readPosition.toJson());
    }

    /*
     * For testing purpose, we are getting dummy highlights from asset. But you can get highlights from your server
     * On success, you can save highlights to FolioReader DB.
     */
    private void getHighlightsAndSave() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                ArrayList<HighLight> highlightList = null;
                ObjectMapper objectMapper = new ObjectMapper();
                try {
                    highlightList = objectMapper.readValue(
                            loadAssetTextAsString("highlights/highlights_data.json"),
                            new TypeReference<List<HighlightData>>() {
                            });
                } catch (IOException e) {
                    e.printStackTrace();
                }

                if (highlightList == null) {
                    folioReader.saveReceivedHighLights(highlightList, new OnSaveHighlight() {
                        @Override
                        public void onFinished() {
                            //You can do anything on successful saving highlight list
                        }
                    });
                }
            }
        }).start();
    }

    private String loadAssetTextAsString(String name) {
        BufferedReader in = null;
        try {
            StringBuilder buf = new StringBuilder();
            InputStream is = getAssets().open(name);
            in = new BufferedReader(new InputStreamReader(is));

            String str;
            boolean isFirst = true;
            while ((str = in.readLine()) != null) {
                if (isFirst)
                    isFirst = false;
                else
                    buf.append('\n');
                buf.append(str);
            }
            return buf.toString();
        } catch (IOException e) {
            Log.e("HomeActivity", "Error opening asset " + name);
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    Log.e("HomeActivity", "Error closing asset " + name);
                }
            }
        }
        return null;
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        FolioReader.clear();
    }

    @Override
    public void onHighlight(HighLight highlight, HighLight.HighLightAction type) {
        Toast.makeText(this,
                "highlight id = " + highlight.getUUID() + " type = " + type,
                Toast.LENGTH_SHORT).show();
    }
}
@@ -1837,35 +1837,8 @@
 der;
-%0A    private Config config;
 %0A%0A  
@@ -2013,223 +2013,8 @@
 );%0A%0A
-        config = AppUtil.getSavedConfig(getApplicationContext());%0A        if (config == null)%0A            config = new Config();%0A        config.setAllowedDirection(Config.AllowedDirection.VERTICAL_AND_HORIZONTAL);%0A%0A
     
@@ -2317,32 +2317,286 @@
 lick(View v) %7B%0A%0A
+                Config config = AppUtil.getSavedConfig(getApplicationContext());%0A                if (config == null)%0A                    config = new Config();%0A                config.setAllowedDirection(Config.AllowedDirection.VERTICAL_AND_HORIZONTAL);%0A%0A
                 
@@ -2854,32 +2854,32 @@
 lick(View v) %7B%0A%0A
-
                 
@@ -2926,24 +2926,278 @@
 osition();%0A%0A
+                Config config = AppUtil.getSavedConfig(getApplicationContext());%0A                if (config == null)%0A                    config = new Config();%0A                config.setAllowedDirection(Config.AllowedDirection.VERTICAL_AND_HORIZONTAL);%0A%0A
             

Revert HomeActivity