package com.benoitletondor.easybudgetapp.view.welcome;


import android.os.Bundle;
import android.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.benoitletondor.easybudgetapp.R;
import com.benoitletondor.easybudgetapp.helper.CurrencyHelper;
import com.benoitletondor.easybudgetapp.model.Expense;
import com.benoitletondor.easybudgetapp.model.db.DB;

import java.util.Date;

/**
 * Onboarding step 3 fragment
 *
 * @author Benoit LETONDOR
 */
public class Onboarding3Fragment extends OnboardingFragment
{
    private TextView moneyTextView;
    private EditText amountEditText;
    private Button nextButton;

// -------------------------------------->

    /**
     * Required empty public constructor
     */
    public Onboarding3Fragment()
    {

    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
    {
        // Inflate the layout for this fragment
        View v = inflater.inflate(R.layout.fragment_onboarding3, container, false);

        DB db = getDB();

        int amount = 0;
        if( db != null )
        {
            amount = db.getBalanceForDay(new Date());
        }

        moneyTextView = (TextView) v.findViewById(R.id.onboarding_screen3_initial_amount_money_tv);
        setCurrency();

        amountEditText = (EditText) v.findViewById(R.id.onboarding_screen3_initial_amount_et);
        amountEditText.setText(String.valueOf(amount));
        amountEditText.addTextChangedListener(new TextWatcher()
        {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after)
            {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count)
            {

            }

            @Override
            public void afterTextChanged(Editable s)
            {
                setButtonText();
            }
        });

        nextButton = (Button) v.findViewById(R.id.onboarding_screen3_next_button);
        nextButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                DB db = getDB();
                if (db != null)
                {
                    int currentBalance = db.getBalanceForDay(new Date());
                    int newBalance = Integer.valueOf(amountEditText.getText().toString());

                    if (newBalance == currentBalance)
                    {
                        // Nothing to do, balance hasn't change
                        return;
                    }

                    int diff = newBalance - currentBalance;

                    final Expense expense = new Expense(getResources().getString(R.string.adjust_balance_expense_title), -diff, new Date());
                    db.persistExpense(expense);
                }

                next(v);
            }
        });
        setButtonText();

        return v;
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser)
    {
        super.setUserVisibleHint(isVisibleToUser);

        if( isVisibleToUser ) // Update values on display
        {
            setCurrency();
            setButtonText();
        }
    }

    @Override
    public int getStatusBarColor()
    {
        return R.color.onboarding_3_statusbar;
    }

// -------------------------------------->

    private void setCurrency()
    {
        if( moneyTextView != null ) // Will be null if view is not yet created
        {
            moneyTextView.setText(CurrencyHelper.getUserCurrency(getActivity()).getSymbol());
        }
    }

    private void setButtonText()
    {
        if( nextButton != null )
        {
            String valueString = amountEditText.getText().toString();
            int value = "".equals(valueString) ? 0 : Integer.valueOf(valueString);

            nextButton.setText(getActivity().getString(R.string.onboarding_start_with_amount, CurrencyHelper.getFormattedCurrencyString(getActivity(), value)));
        }
    }
}

@@ -50,16 +50,48 @@
 come;%0A%0A%0A
+import android.content.Context;%0A
 import a
@@ -104,24 +104,24 @@
 .os.Bundle;%0A
-
 import andro
@@ -293,16 +293,68 @@
 wGroup;%0A
+import android.view.inputmethod.InputMethodManager;%0A
 import a
@@ -483,16 +483,16 @@
 tapp.R;%0A
-
 import c
@@ -542,24 +542,79 @@
 encyHelper;%0A
+import com.benoitletondor.easybudgetapp.helper.Logger;%0A
 import com.b
@@ -2822,17 +2822,17 @@
 Balance 
-=
+!
 = curren
@@ -2871,123 +2871,8 @@
     
-                    // Nothing to do, balance hasn't change%0A                        return;%0A                    %7D%0A%0A
     
@@ -2928,16 +2928,20 @@
 lance;%0A%0A
+    
         
@@ -3093,16 +3093,20 @@
         
+    
 db.persi
@@ -3125,16 +3125,480 @@
 pense);%0A
+                    %7D%0A                %7D%0A%0A                // Hide keyboard%0A                try%0A                %7B%0A                    InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);%0A                    imm.hideSoftInputFromWindow(amountEditText.getWindowToken(), 0);%0A                %7D%0A                catch(Exception e)%0A                %7B%0A                    Logger.error(%22Error while hiding keyboard%22, e);%0A
         

Remove keyboard after onboarding 3