package com.example.moslah_hamza.statsdemo;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;

/**
 * Created by Moslah_Hamza on 03/05/2016.
 */
public class AddActivity extends AppCompatActivity {

    TextView date;
    ArrayList<Categorie> categList =new ArrayList<Categorie>();
    Button addbut;
    TextView category;
    ImageView cat_img;
    EditText sum;
    String id_categ;
    int day ;
    int month ;
    int year ;
    DataBaseTranHandler db;
    Intent i;

    int PROFILE = R.drawable.ic_currency_usd_grey600_18dp;
    RecyclerView mRecyclerView;                           // Declaring RecyclerView
    RecyclerView.Adapter mAdapter;                        // Declaring Adapter For Recycler View
    RecyclerView.LayoutManager mLayoutManager;            // Declaring Layout Manager as a linear layout manager
    String TITLES[] = {"Suivre revenues","Suivre dépenses","Gérer catégories","Planifier","Events","Stats"};
    int ICONS[] = {R.drawable.ic_currency_usd_grey600_18dp,R.drawable.ic_currency_usd_grey600_18dp,R.drawable.ic_wrench_grey600_18dp,
            R.drawable.ic_currency_usd_grey600_18dp, R.drawable.ic_calendar_grey600_18dp,R.drawable.ic_poll_box_grey600_18dp};
    DrawerLayout Drawer;                                  // Declaring DrawerLayout
    ActionBarDrawerToggle mDrawerToggle;                  // Declaring Action Bar Drawer Toggle
    String NAME = "Money Tracker";
    String EMAIL = "";
    DataBaseHandler dbcat;
    String type;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.add_transaction);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        mRecyclerView=new RecyclerView(this);
        mRecyclerView = (RecyclerView) findViewById(R.id.recycler1); // Assigning the RecyclerView Object to the xml View

        mRecyclerView.setHasFixedSize(true);                            // Letting the system know that the list objects are of fixed size
        mAdapter = new MyAdapter(TITLES, ICONS, NAME, EMAIL, PROFILE, new OnItemClickListener() {
            @Override
            public void onItemClick(View item,int position) {
                displayView(position);
                Drawer.closeDrawers();
            }
        });       // Creating the Adapter of MyAdapter class(which we are going to see in a bit)
        // And passing the titles,icons,header view name, header view email,
        // and header view profile picture
        mRecyclerView.setAdapter(mAdapter);                              // Setting the adapter to RecyclerView
        mLayoutManager = new LinearLayoutManager(this);                 // Creating a layout Manager
        mRecyclerView.setLayoutManager(mLayoutManager);                 // Setting the layout Manager

        Drawer = (DrawerLayout) findViewById(R.id.drawer_layout);        // Drawer object Assigned to the view
        mDrawerToggle = new ActionBarDrawerToggle(this,Drawer,toolbar,R.string.openDrawer,R.string.closeDrawer){

            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                // code here will execute once the drawer is opened( As I dont want anything happened whe drawer is
                // open I am not going to put anything here)
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                // Code here will execute once drawer is closed
            }
        }; // Drawer Toggle Object Made
        Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle
        mDrawerToggle.syncState();               // Finally we set the drawer toggle sync State

        // Load Fragment1 when the app starts
        //displayView(1);

        addbut = (Button) findViewById(R.id.add_button);
        sum = (EditText) findViewById(R.id.add_sum);
        db=new DataBaseTranHandler(this);
        db.createdb();

        dbcat=new DataBaseHandler(this);
        dbcat.createdb();
        categList= (ArrayList<Categorie>) dbcat.getAllCategories("Dépense");
        categList.addAll(dbcat.getAllCategories("Revenue"));

        id_categ="Shopping";

        date= (TextView) findViewById(R.id.add_date);
        date.setText(new SimpleDateFormat("EEE d MMM yyyy").format(Calendar.getInstance().getTime()));
        final Calendar now = Calendar.getInstance();
        year = now.get(Calendar.YEAR);
        month =now.get(Calendar.MONTH);
        day = now.get(Calendar.DAY_OF_MONTH);
        final DatePickerDialog dialog = new DatePickerDialog(this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                Calendar c=Calendar.getInstance();
                c.set(year,monthOfYear,dayOfMonth);
                Log.d("month ", String.valueOf(monthOfYear));
                Log.d("day ",String.valueOf(dayOfMonth));
                Log.d("year ",String.valueOf(year));
                date.setText(new SimpleDateFormat("EEE d MMM yyyy").format(c.getTime()));
            }
        },year,month,day);
        //dialog.setContentView(R.layout.dialog_add);
        //dialog.setTitle("Ajouter une nouvelle transaction");
        //final DatePicker datePicker= (DatePicker) dialog.findViewById(R.id.date_add);

        date.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.show();
            }
        });

        /*datePicker.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                day = datePicker.getDayOfMonth();
                month = datePicker.getMonth() + 1;
                year = datePicker.getYear();
                Calendar c=Calendar.getInstance();
                c.set(year,month,day);
                date_add=new SimpleDateFormat("EEE F MMM yyyy").format(c.getTime());
                date.setText(date_add);
                dialog.dismiss();
            }
        });*/

        category= (TextView) findViewById(R.id.category);
        category.setText(categList.get(0).getDescription());
        type=categList.get(0).getType();
        cat_img= (ImageView) findViewById(R.id.img_category);
        cat_img.setImageResource(R.mipmap.ic_launcher);

        final Dialog dialog1 = new Dialog(this);
        dialog1.setContentView(R.layout.dialog_categories);
        dialog1.setTitle("Choisir la catégorie");
        GridView gridView = (GridView) dialog1.findViewById(R.id.grid_view);
        gridView.setAdapter(new ImageAdapter(this, categList));
        category.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog1.show();
            }
        });
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                category.setText(categList.get(position).getDescription());
                id_categ = categList.get(position).getDescription();
                type=categList.get(position).getType();
                dialog1.dismiss();
            }
        });

         i = new Intent(this,MainActivity.class);
        addbut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(type=="Dépense"){
                    db.addDipense(new Transaction(Float.valueOf(sum.getText().toString()), id_categ,
                            date.getText().toString()));
                }else{
                    db.addRevenue(new Transaction(Float.valueOf(sum.getText().toString()), id_categ,
                            date.getText().toString()));
                }

                Log.d("edit text ",sum.getText().toString());
                /*if(!db.getDipense(id_categ,
                        date.getText().toString())
                        .getDate().equals("")){
                    Log.d("Added Sum : ", "sum added successfully");
                }*/
                startActivity(i);
            }
        });
    }

    private void displayView(int position) {
        if(position==3){
            Intent intent=new Intent(AddActivity.this,CategoryAct.class);
            startActivity(intent);
        }
        if(position==1){
            Intent intent=new Intent(AddActivity.this,MainActivity.class);
            startActivity(intent);
        }
    }
}
