package com.algobase.share.activity;


import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.FileObserver;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;


import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Button;
import android.widget.Toast;

import android.net.Uri;

//import com.algobase.share.*;

/*
import androidx.fragment.app.FragmentActivity;
public class AboutActivity extends FragmentActivity 
*/

public class AboutActivity extends Activity 
{

public final static String EXTRA_RESULT = "extra_about_result";

     private File release_file;
     private TextView tv;
     private ScrollView sv;
     private Button but1;
     private Button but2;
     private Button but3;


     void load_file()
     {  	 
        if (release_file == null || !release_file.exists())
        { tv.setText("non-existing file");
          return;
        }

        int buf_size = 512;
        String[] buffer = new String[buf_size];
        int count = 0;

        try 
        { BufferedReader br = new BufferedReader(new FileReader(release_file));
          String line;
          while ((line = br.readLine()) != null && count < buf_size)
               buffer[count++ % buf_size] = line;
          br.close();
        }
        catch (Exception e) {}

        int start = count - buf_size;
        if (start < 0) start = 0;

        for(int i=start; i<count; i++) 
        { tv.append(buffer[i % buf_size]);
          tv.append("\n");
         }

/*
        sv.post(new Runnable() {
           public void run() { sv.fullScroll(View.FOCUS_DOWN); }
        });
*/
        
    }
    
     
    @Override
    public void onCreate(Bundle savedInstanceState) {
                  	   	
        super.onCreate(savedInstanceState);

        Bundle bundle = this.getIntent().getExtras();
        String file_name = bundle.getString("file_name");
        String title = bundle.getString("title");
        final boolean release_notes = title.equals("Release Notes");

        if (file_name != null)
        release_file = new File(file_name);


        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL);
        layout.setBackgroundColor(Color.BLACK);


        tv = new TextView(this);
        tv.setBackgroundColor(Color.BLACK);
        tv.setSingleLine(false);
        tv.setTextSize(16);
        tv.setTextColor(Color.WHITE);
        tv.setTypeface(Typeface.MONOSPACE);

        sv =  new ScrollView(this);
        sv.addView(tv);

        LinearLayout.LayoutParams params2 
        = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0);
        params2.weight = 1.0f;
        layout.addView(sv,params2);


        LinearLayout row = new LinearLayout(this);

        LinearLayout.LayoutParams params1 
        = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.WRAP_CONTENT);
        params1.weight = 1.0f;
        params1.leftMargin = 1;
        params1.rightMargin = 1;

        Button but1 = new Button(this);
        but1.setPadding(0,10,0,10);
        but1.setBackgroundColor(0xfff0f0f0);
        but1.setTextColor(0xff000000);
        but1.setText("Update");
        but1.setTextSize(18);
        row.addView(but1,params1);

        Button but2 = new Button(this);
        but2.setPadding(0,10,0,10);
        but2.setBackgroundColor(0xfff0f0f0);
        but2.setTextColor(0xff000000);
        but2.setText("Uninstall");
        but2.setTextSize(18);
        row.addView(but2,params1);

        Button but3 = new Button(this);
        but3.setPadding(0,10,0,10);
        but3.setBackgroundColor(0xfff0f0f0);
        but3.setTextColor(0xff000000);
        but3.setText("Close");
        but3.setTextSize(18);
        row.addView(but3,params1);

        layout.addView(row);


        setContentView(layout);

        if (release_notes)
          but1.setVisibility(View.INVISIBLE);
        else
        but1.setOnClickListener( new OnClickListener() {
             public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(EXTRA_RESULT,"update");
                setResult(RESULT_OK,intent);
                finish();
             }
        });


        if (release_notes) but2.setText("OK");
        but2.setOnClickListener( new OnClickListener() {
             public void onClick(View view) {
                Intent intent = new Intent();
                if (release_notes)
                  intent.putExtra(EXTRA_RESULT,"cancel");
                else
                  intent.putExtra(EXTRA_RESULT,"uninstall");
                setResult(RESULT_OK,intent);
                finish();
             }
        });

        if (release_notes)
          but3.setVisibility(View.INVISIBLE);
        else
        but3.setOnClickListener( new OnClickListener() {
             public void onClick(View view) {
                Intent intent = new Intent();
                intent.putExtra(EXTRA_RESULT,"cancel");
                setResult(RESULT_OK,intent);
                finish();
             }
        });


        if (title != null) setTitle(title);

        load_file();
    }  

}
