package net.devrand.kihon.installtracker;

import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import net.devrand.kihon.installtracker.event.ViewPackageEvent;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

import butterknife.Bind;
import butterknife.ButterKnife;
import de.greenrobot.event.EventBus;
import okio.BufferedSink;
import okio.BufferedSource;
import okio.Okio;

public class MainActivity extends AppCompatActivity {

    @Bind(R.id.text)
    TextView text;
    @Bind(R.id.read_button)
    Button readButton;
    @Bind(R.id.write_button)
    Button writeButton;
    @Bind(R.id.event_list_fragment)
    FrameLayout fragmentContainer;

    BufferedSink sink = null;

    static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    static final String FILENAME = "/sdcard/installLog.txt";

    static SimpleDateFormat sdf = new SimpleDateFormat(DATE_FORMAT, Locale.US);

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

        ButterKnife.bind(this);

        Fragment fragment = new EventListFragment();
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
        transaction.add(fragmentContainer.getId(), fragment).commit();

        sdf.setTimeZone(TimeZone.getTimeZone("UTC"));

        writeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    CharSequence prefix = text.getCurrentTextColor() == Color.RED ? "" : text.getText();
                    text.setText(prefix + "Appended to the log\n");
                    text.setTextColor(Color.BLUE);

                    sink = Okio.buffer(Okio.appendingSink(new File(FILENAME)));
                    sink.writeUtf8(getLine("something happened"));

                    sink.close();
                    sink = null;
                } catch (IOException ex) {
                    text.setText("Write Error: " + ex.toString());
                    text.setTextColor(Color.RED);
                    ex.printStackTrace();
                    try {
                        if (sink != null) {
                            sink.close();
                            sink = null;
                        }
                    } catch (IOException cex) {
                    }
                }
            }
        });

        readButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                try {
                    text.setTextColor(Color.BLACK);
                    BufferedSource source = Okio.buffer(Okio.source(new File(FILENAME)));
                    text.setText(source.readUtf8());
                    source.close();
                } catch (IOException ex) {
                    text.setText("Read Error: " + ex.toString());
                    text.setTextColor(Color.RED);
                    ex.printStackTrace();
                }
            }
        });

    }

    static String getLine(String string) {
        return String.format("%s == %s\n", sdf.format(new Date(System.currentTimeMillis())), string);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onStart() {
        super.onStart();
        EventBus.getDefault().register(this);
    }

    @Override
    public void onStop() {
        EventBus.getDefault().unregister(this);
        super.onStop();
    }

    public void onEvent(ViewPackageEvent event) {
        String packageName = event.packageName;
        packageName = packageName.startsWith("package:") ? packageName.substring("package:".length()) : packageName;
        PackageManager pm = getPackageManager();
        try {
            PackageInfo info = pm.getPackageInfo(packageName, 0);
            Toast.makeText(this, pm.getApplicationLabel(info.applicationInfo), Toast.LENGTH_SHORT).show();
        } catch (PackageManager.NameNotFoundException ex) {
            ;
        }
    }
}

@@ -4150,104 +4150,53 @@
     
-packageName = packageName.startsWith(%22package:%22) ? packageName.substring(%22package:%22.length()) : 
+Fragment fragment = new PackageEventFragment(
 pack
@@ -4194,32 +4194,33 @@
 ment(packageName
+)
 ;%0A        Packag
@@ -4217,295 +4217,192 @@
     
-PackageManager pm = getPackageManager();%0A        try %7B%0A            PackageInfo info = pm.getPackageInfo(packageName, 0);%0A            Toast.makeText(this, pm.getApplicationLabel(info.applicationInfo), Toast.LENGTH_SHORT).show();%0A        %7D catch (PackageManager.NameNotFoundException ex) %7B
+FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();%0A        transaction.replace(fragmentContainer.getId(), fragment)%0A                .addToBackStack(null)
 %0A   
@@ -4414,19 +4414,22 @@
     
-;%0A
     
-    %7D
+.commit();
 %0A   

load package fragment on event list click