package com.example.moslah_hamza.statsdemo;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
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.view.View;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Moslah_Hamza on 06/05/2016.
 */
public class CategoryAct  extends AppCompatActivity implements  OnItemClickListener {
    private RecyclerView Recycle;
    private CategAdapter myAdapter, myAdapter1;
    private LinearLayoutManager layoutManager;
    private FloatingActionButton AddButton;
    private FloatingActionButton deleteButton;
    List<Categorie> lis;
    Toolbar toolbar;
    DataBaseHandler db;
    ArrayList<Integer> itemChecked;

    int PROFILE = R.drawable.ic_currency_usd_grey600_18dp;
    RecyclerView mRecyclerView;                           // Declaring RecyclerView
    RecyclerView.Adapter drAdapter;                        // 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 = "";

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

        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
        drAdapter= 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(drAdapter);                              // 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);

        Recycle = (RecyclerView) findViewById(R.id.RecyclerId);
        db = new DataBaseHandler(this);
        db.createdb();
        lis= (ArrayList<Categorie>) db.getAllCategories("Dépense");
        lis.addAll(db.getAllCategories("Revenue"));

        myAdapter = new CategAdapter(lis, this);

        Recycle.setAdapter(myAdapter);
        layoutManager = new LinearLayoutManager(this);
        Recycle.setLayoutManager(layoutManager);
        AddButton = (FloatingActionButton) findViewById(R.id.addCategorie);
        AddButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(CategoryAct.this, Ajout.class);
                startActivity(intent);
            }
        });
    }


    @Override
    public void onItemClick(View item, int position) {

    }

    /*private ArrayList<Integer> tri() {
        int n = itemChecked.size();
        int k;
        int temp;
        Log.d("item checked ", itemChecked.toString());
        for (int m = n; m >= 0; m--) {
            for (int i = 0; i < n - 1; i++) {
                k = i + 1;
                if (itemChecked.get(i) < itemChecked.get(k)) {
                    temp = itemChecked.get(k);
                    itemChecked.remove(k);
                    itemChecked.add(i, temp);
                }
            }
        }
        Log.d("item checked ", itemChecked.toString());
        return itemChecked;
    }*/

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