package cancer.findyourfate.com;
 
import android.app.Activity; 
import android.app.AlertDialog;
import android.content.Context; 
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;

import android.text.Html;
import android.util.Log;  
import android.webkit.WebView; 
import android.widget.ImageView;

import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivityfirst extends AppCompatActivity {
      ShareExternalServer appUtil;
     String regId;
     AsyncTask<Void, Void, String> shareRegidTask;
     WebView content,addimagetop;  
	 ImageView img;
	 String statlink;
	 int w;
 	 public static final String MyPREFERENCES = "my" ;
	 public static final String name = "";
	 
    @Override
   public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.firsthome); 
    appUtil = new ShareExternalServer();
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            // Enable the home button and set display options
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME | ActionBar.DISPLAY_SHOW_TITLE | ActionBar.DISPLAY_SHOW_CUSTOM);

            // Set the title of the ActionBar
            actionBar.setTitle(Html.fromHtml("<font color=\"#ffffff\" size=\"21px\"><b>Cancer</b></font>"));

            // Enable the home button for backward compatibility
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
                actionBar.setHomeButtonEnabled(true);
            }

            // Show the ActionBar
            actionBar.show();
        }
    regId = getIntent().getStringExtra("regId");
    
    SharedPreferences prefs =  getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);
    //statlink = prefs.getString(key , "");
    statlink= prefs.getString("key", name);
    
    if((statlink.equals("")) || (statlink.equals("yes")))
    {
      statlink ="yes";
    }
     Log.d("MainActivity", "regId: " + regId);
    Log.d("MainActivity", "status: " + statlink);
    
    final Context context = this;
    shareRegidTask = new AsyncTask<Void, Void, String>() {
      @Override
      protected String doInBackground(Void... params) {
        String result = appUtil.shareRegIdWithAppServer(context, regId,statlink);
        return result;
      }
      @Override
      protected void onPostExecute(String result) {
       shareRegidTask = null;
      }
      };
      
    shareRegidTask.execute(null, null, null);
    final ImageView  img = (ImageView) findViewById(R.id.imageView1);
     Runnable swapImage = new Runnable() { 
		@Override
	    public void run() {
			Intent int_dr1 = new Intent(MainActivityfirst.this,home.class);
			int_dr1.putExtra("REGID", regId);
			int_dr1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(int_dr1);
	    }  
     };
    img.postDelayed(swapImage,2000); 
    }
   
  @Override
 	public void onBackPressed() {
      super.onBackPressed();
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
 	builder.setTitle("Exit!");
 	builder.setMessage("Are you sure you want to exit from Cancer app?")
 	       .setCancelable(false)
 	       .setIcon(R.drawable.alert)
 	       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
 	           public void onClick(DialogInterface dialog, int id) {
 	         	Intent intent = new Intent(Intent.ACTION_MAIN);
 			    	intent.addCategory(Intent.CATEGORY_HOME);
 			    	intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
 			    	startActivity(intent);
 	               }
 	       })
 	       .setNegativeButton("No", new DialogInterface.OnClickListener() {
 	        public void onClick(DialogInterface dialog, int id) {
 	         dialog.cancel();
 	           }
 	       });
 	AlertDialog alert = builder.create();
 	alert.show();
 }
  }

   