package io.github.hidroh.tldroid;

import android.content.res.TypedArray;
import android.database.Cursor;
import android.databinding.DataBindingUtil;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.annotation.AttrRes;
import android.support.annotation.DimenRes;
import android.support.annotation.IdRes;
import android.support.annotation.Nullable;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import com.github.rjeschke.txtmark.Processor;
import com.squareup.okhttp.HttpUrl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;

import java.io.IOException;
import java.lang.ref.WeakReference;

public class CommandActivity extends AppCompatActivity {
    public static final String EXTRA_QUERY = CommandActivity.class.getName() + ".EXTRA_QUERY";
    private static final String FORMAT_HTML_COLOR = "%06X";
    private WebView mWebView;
    private View mProgressBar;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String query = getIntent().getStringExtra(EXTRA_QUERY);
        setTitle(query);
        DataBindingUtil.setContentView(this, R.layout.activity_command);
        setSupportActionBar((Toolbar) findViewById(R.id.toolbar));
        getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME |
                ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_TITLE);
        CollapsingToolbarLayout collapsingToolbar = (CollapsingToolbarLayout)
                findViewById(R.id.collapsing_toolbar_layout);
        collapsingToolbar.setExpandedTitleTypeface(Application.MONOSPACE_TYPEFACE);
        collapsingToolbar.setCollapsedTitleTypeface(Application.MONOSPACE_TYPEFACE);
        mProgressBar = findViewById(R.id.progress);
        mWebView = (WebView) findViewById(R.id.web_view);
        mWebView.setBackgroundColor(ContextCompat.getColor(this,
                getIdRes(android.R.attr.colorBackground)));
        mWebView.setWebViewClient(new WebViewClient() {
            @Override
            public void onPageFinished(WebView view, String url) {
                mProgressBar.setVisibility(View.GONE);
            }
        });
        new GetCommandTask(this).execute(query);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void render(String html) {
        if (TextUtils.isEmpty(html)) {
            return; // TODO
        }
        mWebView.loadDataWithBaseURL(null, wrap(html), "text/html", "UTF-8", null);
    }

    private String wrap(String html) {
        return getString(R.string.command_html,
                html,
                toHtmlColor(android.R.attr.textColorPrimary),
                toHtmlPx(R.dimen.activity_horizontal_margin));
    }

    private String toHtmlColor(@AttrRes int colorAttr) {
        return String.format(FORMAT_HTML_COLOR, 0xFFFFFF &
                ContextCompat.getColor(this, getIdRes(colorAttr)));
    }

    private float toHtmlPx(@DimenRes int dimenAttr) {
        return getResources().getDimension(dimenAttr) / getResources().getDisplayMetrics().density;
    }

    private @IdRes int getIdRes(@AttrRes int attrRes) {
        TypedArray ta = getTheme().obtainStyledAttributes(new int[]{attrRes});
        int resId = ta.getResourceId(0, 0);
        ta.recycle();
        return resId;
    }

    private static class GetCommandTask extends AsyncTask<String, Void, String> {
        private static final String BASE_URL = "https://raw.githubusercontent.com/tldr-pages/tldr/master/pages";
        private final WeakReference<CommandActivity> mCommandActivity;
        private final OkHttpClient mClient;

        public GetCommandTask(CommandActivity commandActivity) {
            mCommandActivity = new WeakReference<>(commandActivity);
            mClient = new OkHttpClient();
        }

        @Override
        protected String doInBackground(String... params) {
            if (mCommandActivity.get() == null) {
                return null;
            }
            Cursor cursor = mCommandActivity.get().getContentResolver()
                    .query(TldrProvider.URI_COMMAND,
                            null,
                            TldrProvider.CommandEntry.COLUMN_NAME + "=?",
                            new String[]{params[0]},
                            null);
            if (cursor == null) {
                return null;
            }
            String platform = null;
            if (cursor.moveToFirst()) {
                platform = cursor.getString(cursor.getColumnIndexOrThrow(
                        TldrProvider.CommandEntry.COLUMN_PLATFORM));
            }
            cursor.close();
            if (TextUtils.isEmpty(platform)) {
                return null;
            }
            String markdown;
            try {
                markdown = mClient.newCall(new Request.Builder()
                        .url(HttpUrl.parse(BASE_URL)
                                .newBuilder()
                                .addPathSegment(platform)
                                .addPathSegment(params[0] + ".md")
                                .build())
                        .build())
                        .execute()
                        .body()
                        .string();
            } catch (IOException e) {
                return null;
            }
            return Processor.process(markdown);
        }

        @Override
        protected void onPostExecute(String s) {
            if (mCommandActivity.get() != null) {
                mCommandActivity.get().render(s);
            }
        }
    }
}

@@ -1146,16 +1146,81 @@
 QUERY%22;%0A
+    private static final String STATE_CONTENT = %22state:content%22;%0A
     priv
@@ -1331,16 +1331,45 @@
 ressBar;
+%0A    private String mContent;
 %0A%0A    @O
@@ -2654,333 +2654,768 @@
     
-new GetCommandTask(this).execute(query);%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        if (item.getItemId() == android.R.id.home) %7B%0A            finish();%0A            return true;%0A        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A    private void render(String html) %7B
+if (savedInstanceState != null) %7B%0A            mContent = savedInstanceState.getString(STATE_CONTENT);%0A        %7D%0A        if (mContent == null) %7B%0A            new GetCommandTask(this).execute(query);%0A        %7D else %7B%0A            render(mContent);%0A        %7D%0A    %7D%0A%0A    @Override%0A    public boolean onOptionsItemSelected(MenuItem item) %7B%0A        if (item.getItemId() == android.R.id.home) %7B%0A            finish();%0A            return true;%0A        %7D%0A        return super.onOptionsItemSelected(item);%0A    %7D%0A%0A    @Override%0A    protected void onSaveInstanceState(Bundle outState) %7B%0A        super.onSaveInstanceState(outState);%0A        outState.putString(STATE_CONTENT, mContent);%0A    %7D%0A%0A    private void render(String html) %7B%0A        mContent = html == null ? %22%22 : html;
 %0A   

Handle orientation change