package com.usai.apex.saved;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;

import com.usai.apex.ApexTrackingApplication;
import com.usai.apex.R;
import com.usai.util.dbUtil;

public class CheckSavedActivity extends AppCompatActivity {
    Fragment f=null;
    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent intent = new Intent();
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                break;
            case R.id.action_deleteall:
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(this);
                builder.setMessage(getString(R.string.str_deletemessage));

                builder.setTitle(getString(R.string.str_confirmdelete));

                builder.setPositiveButton(getString(R.string.str_del),
                        new Dialog.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which)
                            {
                                SQLiteDatabase db = dbUtil.OpenDB(
                                        CheckSavedActivity.this, null, false);
                                String function_name = getIntent().getStringExtra("function_name");
                                if(function_name.equals("Saved Detail"))
                                {

                                    db.execSQL(String.format("delete from favorites where user = '%s'", ApexTrackingApplication.get_user()));
//                                    getSupportFragmentManager().get
//                                    SavedSearchFragment f = (SavedSearchFragment)getSupportFragmentManager().findFragmentById(R.id.tab1);
                                    SavedDetailFragment detail =(SavedDetailFragment) f;
                                    detail.clear_result();
                                    detail.reload_adapter();
                                }
                                else if(function_name.equals("Saved Search"))
                                {
                                    db.execSQL(String.format("delete from history where user = '%s'",ApexTrackingApplication.get_user()));
//                                    SavedDetailFragment f = (SavedDetailFragment)getSupportFragmentManager().findFragmentById(R.id.tab2);

                                    SavedSearchFragment search =(SavedSearchFragment) f;
                                    search.clear_result();
                                    search.reload_adapter();
                                }
                                else if(function_name.equals("Saved Documents"))
                                {
//                                    db.execSQL("delete from favorites");
////                                    SavedDetailFragment f = (SavedDetailFragment)getSupportFragmentManager().findFragmentById(R.id.tab2);
//
//                                    SavedSearchFragment search =(SavedSearchFragment) f;
//                                    search.clear_result();
//                                    search.reload_adapter();
                                    SavedDocumentsFragment search =(SavedDocumentsFragment) f;
                                    ((SavedDocumentsFragment) f).clear();
                                }


                                dbUtil.CloseDB(db);
                                dialog.dismiss();

                            }
                        });

                builder.setNegativeButton(getString(R.string.str_cancel),
                        new Dialog.OnClickListener()
                        {

                            @Override
                            public void onClick(DialogInterface dialog,
                                                int which)
                            {
                                dialog.dismiss();
                            }
                        });

                builder.create().show();

                break;
            }
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.checksaved, menu);
        return true;
    }
    private void setCustomActionBar() {
        ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        View mActionBarView = LayoutInflater.from(this).inflate(R.layout.actionbar_customtitle, null);

        TextView titleview = mActionBarView.findViewById(R.id.title);
        titleview.setText(getIntent().getStringExtra("function_name"));

        setTitle(getIntent().getStringExtra("function_name"));
//
//        mActionBarView.setBackgroundColor(Color.YELLOW);
//        titleview.setBackgroundColor(Color.BLUE);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(mActionBarView, lp);
//        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
//        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);


//        actionBar.setIcon(getNumberDrawable());
//        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(true);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_check_saved);



        if (savedInstanceState == null)
        {


            String function_name = getIntent().getStringExtra("function_name");
            if(function_name.equals("Saved Detail"))
            {
               f = new SavedDetailFragment();
//                ToolsFragment f = new ToolsFragment();
//                Bundle b = new Bundle();
//                b.putBoolean("Login", true);
//                favorites.setArguments(b);
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, f).commit();
            }
            else if (function_name.equals("Saved Search"))
            {
                f = new SavedSearchFragment();
//                Bundle b = new Bundle();
//                b.putBoolean("Login", true);
//                history.setArguments(b);
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, f).commit();
            }
            else if (function_name.equals("Saved Documents"))
            {
                f = new SavedDocumentsFragment();
//                Bundle b = new Bundle();
//                b.putBoolean("Login", true);
//                history.setArguments(b);
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, f).commit();
            }


        }
        setCustomActionBar();
    }
}
