package com.jamesdube.laravelnewsapp.posts;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.github.bkhezry.extrawebview.ExtraWebViewCreator;
import com.github.bkhezry.extrawebview.data.DataModel;
import com.github.bkhezry.extrawebview.data.DataModelBuilder;
import com.jamesdube.laravelnewsapp.App;
import com.jamesdube.laravelnewsapp.R;
import com.jamesdube.laravelnewsapp.SettingsActivity;
import com.jamesdube.laravelnewsapp.adapters.PostAdapter;
import com.jamesdube.laravelnewsapp.models.Post;
import com.jamesdube.laravelnewsapp.util.Themes;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

public class PostActivity extends AppCompatActivity {
    WebView webView;
    TextView postTitle,pubDate;
    ImageView postImage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Themes.applyTheme(this);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_post);
        boot();


        Post post = getPost();

        //set the title
        postTitle.setText(post.getTitle());

        //set the pub date
        pubDate.setText(post.getPubDate());

        //set the Image
        Glide.with(App.getAppContext())
                .load(post.getCoverImage())
                .into(postImage);

        //load the content
        webView.loadData(prepHtml(post), "text/html", "UTF-8");

    }

    private void loadWebView(Post post){
        try{
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date parsedDate = dateFormat.parse(post.getPubDate());
            DataModel dataModel = new DataModelBuilder()
                    .withType("blog")
                    .withBy("authorName")
                    .withTime(parsedDate.getTime())
                    .withUrl(post.getLink())
                    .withDescription(post.getTitle())
                    .withBookmark(true)
                    .withViewed(true)
                    .withRank(0)
                    .withVoted(true)
                    .withPageTitle(post.getTitle())
                    .build();
            new ExtraWebViewCreator()
                    .withContext(this)
                    .withBookmarkIcon(true)
                    .withVoteIcon(true)
                    //.withCustomFont("fonts/IRANSansMobile.ttf")
                    //.withThemeName(themeName)
                    .withDataModel(dataModel)
                    .show();
        }catch(Exception e){//this generic but you can control another types of exception

        }


    }

    private Post getPost(){
        Bundle getBundle = null;
        getBundle = getIntent().getExtras();

        String post = getBundle.getString("POST");
        return Post.fromJson(post);
    }

    private String prepHtml(Post post){
        String html = post.getDescription().replaceFirst("<img([^<]*)>", "");
        Log.d(App.Tag,html);
        return html;
    }

    private void boot(){
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        assert getSupportActionBar() != null;
         getSupportActionBar().setHomeButtonEnabled(true);
        postTitle = (TextView) findViewById(R.id.postTitle);
        pubDate = (TextView) findViewById(R.id.pubDate);
        postImage = (ImageView) findViewById(R.id.backdrop);
        webView = (WebView) findViewById(R.id.webView) ;
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_post, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            startActivity(new Intent(App.getAppContext(),SettingsActivity.class));
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

@@ -1769,24 +1769,63 @@
 ostImage);%0A%0A
+        String html = prepHtml(post);%0A%0A
         //lo
@@ -1867,23 +1867,50 @@
 Data
-(prepHtml(post)
+WithBaseURL(%22file:///android_asset/%22, html
 , %22t
@@ -1925,14 +1925,20 @@
 %22, %22
-UTF-8%22
+utf-8%22, null
 );%0A%0A
@@ -3370,29 +3370,228 @@
         
-String html =
+return %22%3CHTML%3E%3CHEAD%3E%3Clink href=%5C%22style.css%5C%22 type=%5C%22text/css%5C%22 rel=%5C%22stylesheet%5C%22/%3E%3C/HEAD%3E%3Cbody%3E%22 +%0A                %22%3Cdiv class=%5C%22container-fluid%5C%22%3E%3Cdiv class=%5C%22row%5C%22%3E %3Cdiv class=%5C%22col-lg-12%5C%22%3E%22 +%0A               
  post.ge
@@ -3637,17 +3637,18 @@
 )%3E%22, %22%22)
-;
+ +
 %0A       
@@ -3652,48 +3652,51 @@
     
-Log.d(App.Tag,html);%0A        return html
+        %22 %3C/div%3E%3C/div%3E%3C/div%3E%3C/body%3E%3C/HTML%3E%22
 ;%0A  

update html