package com.team_clicker.idlefarmer;

import android.content.DialogInterface;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.team_clicker.idlefarmer.adapter.CerealAdapter;
import com.team_clicker.idlefarmer.model.Cereal;
import com.team_clicker.idlefarmer.runnable.CerealRun;
import com.team_clicker.idlefarmer.service.GameService;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Timer;

/**

 * An example full-screen activity that shows and hides the system UI (i.e.

 * status bar and navigation/system bar) with user interaction.

 */

/**
 * TODO OnPause, OnResume
 */
public class MainActivity extends AppCompatActivity {
    public final static int MESSAGE_MONEY = 1;

    private GameService service = new GameService(this);
    private TextView tvMoney;
    private ListView listViewCereals;
    private boolean fromOnCreate = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        tvMoney = (TextView) findViewById(R.id.valueMoney);

        listViewCereals = (ListView)findViewById(R.id.cereals);
        listViewCereals.setOnItemClickListener(cerealClick);

        fromOnCreate = true;
    }

    @Override
    protected void onPause() {
        super.onPause();

        Calendar now = Calendar.getInstance();

        service.getGame().setDbtIdle(now.getTimeInMillis());

        service.saveGame();
    }

    @Override
    protected void onResume() {
        super.onResume();

        service.init();

        if(service.getGame().getDbtIdle() > 0){
            Calendar now = Calendar.getInstance();
            long diffSeconds = (now.getTimeInMillis() - service.getGame().getDbtIdle()) / 1000;
            double moneyEarns = diffSeconds * service.getGame().getEarnBySeconds();

            dialog(moneyEarns);

            service.getGame().addMoney(moneyEarns);
        }

        setTextMoneyTV(service.getGame().getMoney());

        if(fromOnCreate) {
            for(Cereal cereal : service.getGame().getCereals()){
                if(cereal.getLevel() > 0){
                    Timer timer = new Timer();
                    timer.schedule(new CerealRun(cereal, service.getGame(), moneyHandler), 0, cereal.getGrowthTime()*1000);
                }
            }

            fromOnCreate = false;
        }

        List<Cereal> cerealsList = service.getGame().getCereals();

        CerealAdapter adapter = new CerealAdapter(getApplicationContext(), cerealsList);
        listViewCereals.setAdapter(adapter);
    }

    private AdapterView.OnItemClickListener cerealClick = new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Cereal entry = (Cereal)parent.getAdapter().getItem(position);

            if(service.getGame().getMoney() >= entry.getCurrentPrice()){
                entry.setLevel(entry.getLevel() + 1);

                if(entry.getLevel() == 1){
                    Timer timer = new Timer();
                    timer.schedule(new CerealRun(entry, service.getGame(), moneyHandler), 0, entry.getGrowthTime()*1000);
                }

                service.getGame().removeMoney(entry.getCurrentPrice());
                service.updateCereal(entry);

                setTextMoneyTV(service.getGame().getMoney());

                listViewCereals.invalidateViews();

                listViewCereals.refreshDrawableState();
            }
        }
    };

    private void dialog(double money){
        String moneyString = "";
        if(money > 999999){
            moneyString = String.format("%.2e",money) + "$";
        } else {
            moneyString = String.format("%.2f",money) + "$";
        }

        AlertDialog.Builder dialog = new AlertDialog.Builder(MainActivity.this);
        dialog.setCancelable(false);
        dialog.setTitle("Argent Gagné");
        dialog.setMessage("Vous avez gagne " + moneyString);
        dialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int id) {
            }
        });

        AlertDialog alert = dialog.create();
        alert.show();
    }

    private Handler moneyHandler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if(msg.what == MESSAGE_MONEY){
                double money = (double)msg.obj;
                setTextMoneyTV(money);
            }
        }
    };

    private void setTextMoneyTV(double money){
        if(money > 999999){
            tvMoney.setText(String.format("%.2e",money) + "$");
        } else {
            tvMoney.setText(String.format("%.2f",money) + "$");
        }
    }
}
@@ -644,32 +644,60 @@
 mpleDateFormat;%0D
+import java.util.ArrayList;%0D
 import java.util
@@ -1207,37 +1207,48 @@
 ate 
-boolean fromOnCreate = false;
+List%3CTimer%3E timers = new ArrayList%3C%3E();%0D
 %0D%0D  
@@ -1600,38 +1600,8 @@
 k);%0D
-%0D        fromOnCreate = true;%0D
     
@@ -1780,24 +1780,98 @@
 Millis());%0D%0D
+        for(Timer timer : timers)%7B%0D            timer.cancel();%0D        %7D%0D%0D
         serv
@@ -2426,39 +2426,8 @@
 );%0D%0D
-        if(fromOnCreate) %7B%0D    
     
@@ -2495,20 +2495,16 @@
         
-    
 if(cerea
@@ -2530,36 +2530,32 @@
                 
- 
-   
 Timer timer = ne
@@ -2557,36 +2557,32 @@
  = new Timer();%0D
-    
                 
@@ -2705,58 +2705,40 @@
     
-%7D%0D            %7D%0D%0D            fromOnCreate = false;
+timers.add(timer);%0D            %7D
 %0D   
@@ -3568,32 +3568,71 @@
 thTime()*1000);%0D
+                    timers.add(timer);%0D
                 

Fix Bug Money