package auspost.com.au.hackday;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import auspost.com.au.hackday.model.User;
import auspost.com.au.hackday.persistence.DatabaseManager;
import auspost.com.au.hackday.utils.UserUtils;

import java.io.InputStream;


public class MainActivity extends ActionBarActivity {

    private ServicesListAdapter formsListAdapter;

    private ListView formListView;
    private ImageButton profilePageButton;
    private TextView profilePer;
    private DatabaseManager databaseManager;
    private User user;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        user = getIntent().getParcelableExtra("user");
        setContentView(R.layout.activity_main);
        formsListAdapter = new ServicesListAdapter(this.getApplicationContext());
        formListView = (ListView) findViewById(R.id.list_forms);
        formListView.setAdapter(formsListAdapter);
        profilePageButton();
        verificationPercentage();

        if (user.changeAddress.equalsIgnoreCase("true")) {
            formsListAdapter.update("COAN", "Green", true);
        } else {
            formsListAdapter.update("COAN", "Amber", false);
        }
        formsListAdapter.update("Mypost", "Amber", false);
        formsListAdapter.update("MRSO", "Red", false);
        formsListAdapter.update("Passport", "Black", false);
        formsListAdapter.update("Postal Vote", "Green", true);
        formsListAdapter.update("Land Title", "Red", false);
        formsListAdapter.update("Mypost Card", "Amber", false);
        formsListAdapter.update("Concession Card", "Red", false);
        formsListAdapter.update("MPDM", "Green", true);
    }

    private void verificationPercentage() {
        profilePer = (TextView) findViewById(R.id.verifiedPer);
        profilePer.setText(user.verificationPercentage + "%");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    private void profilePageButton() {
        profilePageButton = (ImageButton) findViewById(R.id.profile_image);
        new DownloadImageTask(profilePageButton)
                .execute("http://wp.patheos.com.s3.amazonaws.com/blogs/tinseltalk/files/2012/05/280px-Tony_Stark_Avengers-150x150.png");
        profilePageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, ProfileActivity.class);
                startActivity(intent);
            }
        });
    }

    private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {

        ImageView bmImage;

        public DownloadImageTask(ImageView bmImage) {
            this.bmImage = bmImage;
        }

        protected Bitmap doInBackground(String... urls) {
            String urldisplay = urls[0];
            Bitmap mIcon11 = null;
            try {
                InputStream in = new java.net.URL(urldisplay).openStream();
                mIcon11 = BitmapFactory.decodeStream(in);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return mIcon11;
        }

        protected void onPostExecute(Bitmap result) {
            bmImage.setImageBitmap(result);
        }
    }
}

@@ -1485,27 +1485,40 @@
 er.update(%22C
-OAN
+hange of address
 %22, %22Green%22, 
@@ -1583,11 +1583,24 @@
 e(%22C
-OAN
+hange of address
 %22, %22
@@ -1658,22 +1658,25 @@
 update(%22
-Mypost
+Hold mail
 %22, %22Ambe
@@ -1724,12 +1724,32 @@
 te(%22
-MRSO
+Apply for a bank account
 %22, %22
@@ -1795,17 +1795,29 @@
 update(%22
-P
+Apply for a p
 assport%22
@@ -1875,17 +1875,17 @@
 %22Postal 
-V
+v
 ote%22, %22G
@@ -1935,218 +1935,39 @@
 te(%22
-Land Title%22, %22Red%22, false);%0A        formsListAdapter.update(%22Mypost Card%22, %22Amber%22, false);%0A        formsListAdapter.update(%22Concession Card%22, %22Red%22, false);%0A        formsListAdapter.update(%22MPDM%22, %22Green%22, tru
+Update land title%22, %22Red%22, fals
 e);%0A

update the name of the services.