package com.kwc.kantinesaldo;

import android.app.Activity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class SaldoActivity extends Activity {

    private static final String TAG = "kantinesaldo";
    private static final String CARD_NUMBER = "card_number";
    private static final String CARD_PIN = "card_pin";
    private static final String STATE_BALANCE = "balance";
    private static final String STATE_CARDINFO_SHOWING = "cardinfo_showing";
    private TextView balanceView;
    private Button updateButton;
    private SharedPreferences prefs;
    private String balance;
    private CardInfoDialogFragment cardInfoDialogFragment;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefs = getPreferences(MODE_PRIVATE);
        setContentView(R.layout.main);

        balanceView = (TextView) findViewById(R.id.balanceView);


        updateButton = (Button) findViewById(R.id.refreshBalanceButton);
        updateButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String cardNumber = getCardNumber();
                String pin = getPin();

                if (cardNumber != null && !cardNumber.isEmpty() || pin != null && !pin.isEmpty()) {
                    HttpGetBalance httpGetBalance = new HttpGetBalance(getApplicationContext(), cardNumber, pin) {

                        @Override
                        public void onResult(String balance) {
                            SaldoActivity.this.balance = balance;
                            balanceView.setText(balance);
                            updateButton.setEnabled(true);
                        }
                    };
                    httpGetBalance.execute();
                    updateButton.setEnabled(false);
                } else {
                    showCardInfoDialog();
                }
            }
        });
    }

    private void showCardInfoDialog() {
        cardInfoDialogFragment = new CardInfoDialogFragment(getCardNumber(), getPin()) {

            @Override
            protected void saveCardInfo(String cardNumber, String pin) {
                SharedPreferences.Editor editor = prefs.edit();
                editor.putString(CARD_NUMBER, cardNumber);
                editor.putString(CARD_PIN, pin);
                editor.commit();
            }
        };
        cardInfoDialogFragment.show(getFragmentManager(), TAG);
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mainmenu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.action_settings) {
            showCardInfoDialog();
        }
        return true;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString(STATE_BALANCE, balance);

        if (cardInfoDialogFragment != null && cardInfoDialogFragment.getDialog() != null) {
            outState.putBoolean(STATE_CARDINFO_SHOWING, true);
        } else {
            outState.putBoolean(STATE_CARDINFO_SHOWING, false);
        }
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if (savedInstanceState != null) {
            this.balance = savedInstanceState.getString(STATE_BALANCE);
            if (savedInstanceState.getBoolean(STATE_CARDINFO_SHOWING)) {
                showCardInfoDialog();
            }
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (balance != null) {
            balanceView.setText(balance);
        }
    }

    private String getPin() {
        return prefs.getString(CARD_PIN, null);
    }

    private String getCardNumber() {
        return prefs.getString(CARD_NUMBER, null);
    }

}

@@ -429,24 +429,29 @@
 inal String 
+PREF_
 CARD_NUMBER 
@@ -499,16 +499,21 @@
  String 
+PREF_
 CARD_PIN
@@ -527,16 +527,74 @@
 d_pin%22;%0A
+    private static final String PREF_BALANCE = %22balance%22;%0A
     priv
@@ -1176,16 +1176,64 @@
 eView);%0A
+        balanceView.setText(getSavedBalance());%0A
 %0A%0A      
@@ -1470,24 +1470,29 @@
 Number = get
+Saved
 CardNumber()
@@ -1521,24 +1521,29 @@
 ng pin = get
+Saved
 Pin();%0A%0A    
@@ -1913,24 +1913,91 @@
  = balance;%0A
+                            savePreference(PREF_BALANCE, balance);%0A
             
@@ -2469,16 +2469,21 @@
 ment(get
+Saved
 CardNumb
@@ -2487,24 +2487,29 @@
 umber(), get
+Saved
 Pin()) %7B%0A%0A  
@@ -2617,22 +2617,20 @@
         
-Shared
+save
 Preferen
@@ -2635,178 +2635,89 @@
 ence
-s.Editor editor = prefs.edit();%0A                editor.putString(CARD_NUMBER, cardNumber);%0A                editor.putString(CARD_PIN, pin);%0A                editor.commit(
+(PREF_CARD_NUMBER, cardNumber);%0A                savePreference(PREF_CARD_PIN, pin
 );%0A 
@@ -4203,26 +4203,217 @@
 private 
-String get
+void savePreference(String key, String value) %7B%0A        SharedPreferences.Editor editor = prefs.edit();%0A        editor.putString(key, value);%0A        editor.commit();%0A    %7D%0A%0A    private String getSaved
 Pin() %7B%0A
@@ -4439,24 +4439,29 @@
 s.getString(
+PREF_
 CARD_PIN, nu
@@ -4494,16 +4494,21 @@
 ring get
+Saved
 CardNumb
@@ -4545,16 +4545,21 @@
 tString(
+PREF_
 CARD_NUM
@@ -4577,10 +4577,108 @@
 %0A    %7D%0A%0A
+    private String getSavedBalance() %7B%0A        return prefs.getString(PREF_BALANCE, null);%0A    %7D%0A%0A
 %7D%0A

Save balance and show on startup