<commit_before>package com.hifivesoccer.activities;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hifivesoccer.R;
import com.hifivesoccer.models.Game;
import com.hifivesoccer.utils.MySelf;
import com.hifivesoccer.utils.ServerHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class NewGameActivity extends AppActivity {

    private static final String TAG = NewGameActivity.class.getSimpleName();
    private final Context context = this;

    private Toolbar toolbar;

    String friendsId = "";
    String friendsName = "";

    JSONArray pendingList = new JSONArray();

    private final ServerHandler server = ServerHandler.getInstance(context);

    static final int DIALOG_DATE_ID = 0;
    static final int DIALOG_TIME_ID = 1;

    private TextView button_date;
    int year_x;
    int month_x;
    int day_x;

    private TextView button_time;
    int hours_x;
    int min_x;

    private Calendar cal;

    private Date gameDate;
    private Date gameTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_game);

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        final Button createButton = (Button) findViewById(R.id.new_match);

        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String placeName = getEditTextValue(R.id.place_name);
                final String placeAddress = getEditTextValue(R.id.place_address);
                final String gamePrice = getEditTextValue(R.id.game_price);
                final String gameDescription = getEditTextValue(R.id.game_description);
                Switch isPrivateSwitch = (Switch) findViewById(R.id.private_game);
                final boolean isPrivate = isPrivateSwitch.isEnabled();

                String postGameDate = new SimpleDateFormat("EEE dd MMM yyyy", Locale.getDefault()).format(gameDate);
                String postGameTime = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(cal.getTime());

                if (!placeName.isEmpty() && !placeAddress.isEmpty() && !gamePrice.isEmpty()) {

                    JSONObject json = new JSONObject();
                    try {
                        json.put("organizer", MySelf.getSelf().get_id());
                        json.put("description", gameDescription);
                        json.put("date", postGameDate);
                        json.put("time", postGameTime);
                        json.put("place", placeName);
                        json.put("price", Float.parseFloat(gamePrice));
                        json.put("private", isPrivate);

                        if (pendingList.length() > 0)
                            json.put("pending", pendingList);

                        Log.d(TAG, json.toString());

                        server.postDatas("game", json, new ServerHandler.ResponseHandler() {
                            @Override
                            public void onSuccess(Object response) {
                                Log.d(TAG, response.toString());

                                Toast toast = Toast.makeText(context, "Game bien ajouté", Toast.LENGTH_SHORT);
                                toast.show();

                                //Intent intent = new Intent(context, GameDetailActivity.class);
                                //intent.putExtra("GAME_ID", gameList.get(position).get_id());
                                //startActivity(intent);
                            }

                            @Override
                            public void onError(String error){
                                Log.e(TAG, error);
                                Toast toast = Toast.makeText(context, R.string.hifive_generic_error, Toast.LENGTH_SHORT);
                                toast.show();
                            }
                        });

                    } catch (JSONException e) {
                        Log.e(TAG, e.toString());
                        Toast toast = Toast.makeText(context, R.string.hifive_generic_error, Toast.LENGTH_SHORT);
                        toast.show();
                    }

                } else {
                    Toast toast = Toast.makeText(context, R.string.from_empty, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        cal = Calendar.getInstance(Locale.getDefault());

        TimeZone tz = TimeZone.getDefault();
        cal.setTimeZone(tz);

        year_x = cal.get(Calendar.YEAR);
        month_x = cal.get(Calendar.MONTH);
        day_x = cal.get(Calendar.DATE);

        hours_x = cal.get(Calendar.HOUR_OF_DAY);
        min_x = cal.get(Calendar.MINUTE);

        showDialogOnButtonClick();
        showTimePickerDialog();

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
            case DIALOG_DATE_ID:
                return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
            case DIALOG_TIME_ID:
                return new TimePickerDialog(this, tpickerListner, hours_x, min_x, true);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener dpickerListner = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            year_x = year;
            month_x = monthOfYear + 1;<commit_msg>Fix date<commit_after>package com.hifivesoccer.activities;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.app.TimePickerDialog;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NavUtils;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.Toast;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hifivesoccer.R;
import com.hifivesoccer.models.Game;
import com.hifivesoccer.utils.MySelf;
import com.hifivesoccer.utils.ServerHandler;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;

public class NewGameActivity extends AppActivity {

    private static final String TAG = NewGameActivity.class.getSimpleName();
    private final Context context = this;

    private Toolbar toolbar;

    String friendsId = "";
    String friendsName = "";

    JSONArray pendingList = new JSONArray();

    private final ServerHandler server = ServerHandler.getInstance(context);

    static final int DIALOG_DATE_ID = 0;
    static final int DIALOG_TIME_ID = 1;

    private TextView button_date;
    int year_x;
    int month_x;
    int day_x;

    private TextView button_time;
    int hours_x;
    int min_x;

    private Calendar cal;

    private Date gameDate;
    private Date gameTime;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_game);

        toolbar = (Toolbar) findViewById(R.id.tool_bar);
        setSupportActionBar(toolbar);

        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);
        }

        final Button createButton = (Button) findViewById(R.id.new_match);

        createButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String placeName = getEditTextValue(R.id.place_name);
                final String placeAddress = getEditTextValue(R.id.place_address);
                final String gamePrice = getEditTextValue(R.id.game_price);
                final String gameDescription = getEditTextValue(R.id.game_description);
                Switch isPrivateSwitch = (Switch) findViewById(R.id.private_game);
                final boolean isPrivate = isPrivateSwitch.isEnabled();

                String postGameDate = new SimpleDateFormat("EEE dd MMM yyyy", Locale.getDefault()).format(gameDate);
                String postGameTime = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(gameTime);

                if (!placeName.isEmpty() && !placeAddress.isEmpty() && !gamePrice.isEmpty()) {

                    JSONObject json = new JSONObject();
                    try {
                        json.put("organizer", MySelf.getSelf().get_id());
                        json.put("description", gameDescription);
                        json.put("date", postGameDate);
                        json.put("time", postGameTime);
                        json.put("place", placeName);
                        json.put("price", Float.parseFloat(gamePrice));
                        json.put("private", isPrivate);

                        if (pendingList.length() > 0)
                            json.put("pending", pendingList);

                        Log.d(TAG, json.toString());

                        final ProgressDialog progress = new ProgressDialog(context);
                        progress.setMessage(context.getString(R.string.loading));
                        progress.show();

                        server.postDatas("game", json, new ServerHandler.ResponseHandler() {
                            @Override
                            public void onSuccess(Object response) {
                                Log.d(TAG, response.toString());

                                progress.dismiss();

                                JSONObject res = (JSONObject) response;

                                try {
                                    String id = res.getString("id");

                                    Intent intent = new Intent(context, GameDetailActivity.class);
                                    intent.putExtra("GAME_ID", id);
                                    startActivity(intent);
                                    finish();

                                } catch (JSONException e) {
                                    Log.e(TAG, e.toString());

                                    Intent intent = new Intent(context, MainActivity.class);
                                    startActivity(intent);
                                    finish();

                                    e.printStackTrace();
                                }

                                //Toast toast = Toast.makeText(context, "Game bien ajouté", Toast.LENGTH_SHORT);
                                //toast.show();
                            }

                            @Override
                            public void onError(String error){
                                Log.e(TAG, error);
                                Toast toast = Toast.makeText(context, R.string.hifive_generic_error, Toast.LENGTH_SHORT);
                                toast.show();
                                progress.dismiss();
                            }
                        });

                    } catch (JSONException e) {
                        Log.e(TAG, e.toString());
                        Toast toast = Toast.makeText(context, R.string.hifive_generic_error, Toast.LENGTH_SHORT);
                        toast.show();
                    }

                } else {
                    Toast toast = Toast.makeText(context, R.string.from_empty, Toast.LENGTH_SHORT);
                    toast.show();
                }
            }
        });

        cal = Calendar.getInstance(Locale.getDefault());

        TimeZone tz = TimeZone.getDefault();
        cal.setTimeZone(tz);

        year_x = cal.get(Calendar.YEAR);
        month_x = cal.get(Calendar.MONTH);
        day_x = cal.get(Calendar.DATE);

        hours_x = cal.get(Calendar.HOUR_OF_DAY);
        min_x = cal.get(Calendar.MINUTE);

        gameDate = cal.getTime();
        gameTime = cal.getTime();

        showDialogOnButtonClick();
        showTimePickerDialog();

    }

    @Override
    protected Dialog onCreateDialog(int id) {
        switch(id) {
            case DIALOG_DATE_ID:
                return new DatePickerDialog(this, dpickerListner, year_x, month_x, day_x);
            case DIALOG_TIME_ID:
                return new TimePickerDialog(this, tpickerListner, hours_x, min_x, true);
        }
        return null;
    }

    private DatePickerDialog.OnDateSetListener dpickerListner = new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            year_x = year;
            month_x = monthOfYear + 1;
{'fraction_non_alphanumeric': 0.09279835390946502, 'fraction_numerical': 0.0012345679012345679, 'mean_word_length': 1.8839003164556962, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 11, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6079044', 'n_tokens_mistral': 3859, 'n_tokens_neox': 3626, 'n_words': 765}