package com.astoev.cave.survey.activity.dialog;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import android.text.Html;
import android.text.method.LinkMovementMethod;
import android.text.util.Linkify;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.astoev.cave.survey.BuildConfig;
import com.astoev.cave.survey.Constants;
import com.astoev.cave.survey.R;
import com.astoev.cave.survey.util.ConfigUtil;
import com.astoev.cave.survey.util.StringUtils;

import java.io.IOException;
import java.util.Date;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;

/**
 * Fragment that shows about dialog
 *
 * @author Zhivko Mitrev
 */
public class AboutDialog extends DialogFragment  {

    /**
     * @see DialogFragment#onCreateDialog(Bundle)
     */
    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {

        Activity activity = getActivity();
        Context context = getActivity().getApplicationContext();

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);

        TextView titleView = new TextView(context);
        titleView.setText(R.string.about_title);
        titleView.setGravity(Gravity.CENTER);
        titleView.setPadding(10, 10, 10, 10);
        titleView.setTextSize(22);
        titleView.setTextColor(Color.WHITE);
        builder.setCustomTitle(titleView);

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View view = inflater.inflate(R.layout.about, null);
        builder.setView(view);

        TextView url = (TextView) view.findViewById(R.id.aboutUrl);
        Linkify.addLinks(url, Linkify.WEB_URLS);

        StringBuilder versionText = new StringBuilder("v");
        try {
            versionText.append(context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName);
        } catch (PackageManager.NameNotFoundException e) {
            Log.e(Constants.LOG_TAG_UI, "Failed get version for about dialog", e);
        }

        String buildDate = getBuildDate();
        versionText.append(buildDate);

        if (BuildConfig.DEBUG) {
            versionText.append(" (Debug)");
        }

        TextView version = (TextView) view.findViewById(R.id.aboutVersion);
        version.setText(versionText.toString());

        StringBuilder userGuideText = new StringBuilder("<a href=\"");
        userGuideText.append(getString(R.string.about_help));
        userGuideText.append("\">");
        userGuideText.append(getString(R.string.about_user_guide));
        userGuideText.append("</a>");
        TextView userGuide = (TextView) view.findViewById(R.id.aboutUserGuide);
        userGuide.setText(Html.fromHtml(userGuideText.toString()));
        userGuide.setMovementMethod(LinkMovementMethod.getInstance());

        return  builder.create();
    }

    // kind of ugly build date looking at the time the dex files were last updated
    private String getBuildDate() {
        Activity context = ConfigUtil.getContext();
        ZipFile zf = null;
        try {
            ApplicationInfo ai = context.getPackageManager().getApplicationInfo(context.getPackageName(), 0);
            zf = new ZipFile(ai.sourceDir);
            ZipEntry ze = zf.getEntry("classes.dex");
            long time = ze.getTime();
            return " / " + StringUtils.dateToString(new Date(time));

        } catch(Exception e) {
            Log.e(Constants.LOG_TAG_UI, "Failed get build date", e);
            return "";
        } finally {
            if (zf != null) {
                try {
                    zf.close();
                } catch (IOException e) {
                    Log.e(Constants.LOG_TAG_UI, "Failed close file", e);
                }
            }
        }
    }

}

@@ -188,27 +188,23 @@
 tent.pm.
-Application
+Package
 Info;%0Aim
@@ -813,137 +813,42 @@
 il;%0A
-import com.astoev.cave.survey.util.StringUtils;%0A%0Aimport java.io.IOException;%0Aimport java.util.Date;%0Aimport java.util.zip.ZipEntry
+%0Aimport java.text.SimpleDateFormat
 ;%0Aim
@@ -866,18 +866,11 @@
 til.
-zip.ZipFil
+Dat
 e;%0A%0A
@@ -3110,90 +3110,8 @@
  %7D%0A%0A
-    // kind of ugly build date looking at the time the dex files were last updated
 %0A   
@@ -3155,25 +3155,36 @@
     
-Activity c
+try %7B%0A            C
 ontext 
+c 
 = Co
@@ -3218,81 +3218,39 @@
     
-ZipFile zf = null;%0A        try %7B%0A            ApplicationInfo ai = context
+    PackageInfo packageInfo = c
 .get
@@ -3273,31 +3273,21 @@
 .get
-ApplicationInfo(context
+PackageInfo(c
 .get
@@ -3321,129 +3321,108 @@
     
-zf = new ZipFile(ai.sourceDir);%0A            ZipEntry ze = zf.getEntry(%22classes.dex%22);%0A            long time = ze.get
+String appBuildDate = SimpleDateFormat.getDateInstance().format(new Date(packageInfo.lastUpdate
 Time
-(
+)
 );%0A 
@@ -3451,67 +3451,66 @@
 %22 + 
-StringUtils.dateToString(new Date(time));%0A%0A        %7D catch(
+appBuildDate;%0A        %7D catch (PackageManager.NameNotFound
 Exce
@@ -3564,24 +3564,27 @@
 UI, %22Failed 
+to 
 get build da
@@ -3619,259 +3619,8 @@
 %22%22;%0A
-        %7D finally %7B%0A            if (zf != null) %7B%0A                try %7B%0A                    zf.close();%0A                %7D catch (IOException e) %7B%0A                    Log.e(Constants.LOG_TAG_UI, %22Failed close file%22, e);%0A                %7D%0A            %7D%0A
     

fix build date in about dialog