package com.example.moslah_hamza.statsdemo;

import android.content.Intent;
import android.os.Bundle;
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.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import java.util.List;

public class Edit extends AppCompatActivity {
    private String id, id2;
    private Button editBt;
    private EditText edit;
    private RadioGroup radioGroup;
    private int indice;
    private DataBaseHandler db;
    private Categorie cat;
    private String type;

    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.main_edit_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

        Bundle extras = getIntent().getExtras();
        id = extras.getString("TAG");
        indice = extras.getInt("TAG2");
        type = extras.getString("TAG3");
        edit = (EditText) findViewById(R.id.editText);
        edit.setText(id);
        radioGroup = (RadioGroup) findViewById(R.id.groupe_radio);
        if (1 == indice) {
            radioGroup.check(R.id.radioButton);
        } else if (0 == indice) {
            radioGroup.check(R.id.radioButton2);
        } else {
            radioGroup.check(R.id.radioButton3);
        }
        cat = new Categorie();
       //edit
        editBt = (Button) findViewById(R.id.button);
        db = new DataBaseHandler(this);
        editBt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                indice = radioGroup.getCheckedRadioButtonId();
                if (R.id.radioButton == indice) {
                    indice = 1;
                } else if (R.id.radioButton2 == indice) {
                    indice = 0;
                } else {
                    indice = -1;
                }

                cat.setDescription(edit.getText().toString());
                cat.setPriority(indice);
                cat.setType(type);
                db.updateCategorie(cat, id);

                Intent intent = new Intent(Edit.this, CategoryAct.class);
                Toast.makeText(getApplicationContext(), "Categorie update successfully", Toast.LENGTH_LONG).show();
                startActivity(intent);
            }
        });
    }

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