package com.raataar.weisheiten_allinone;

import android.app.ActionBar;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;

import com.android.volley.toolbox.ImageLoader;
import com.raataar.weisheiten_allinone.app.AppController;

import java.lang.reflect.Method;


public class SuchenActivity extends Activity {
    // Log tag
    private static final String TAG = SuchenActivity.class.getSimpleName();

    // Json url
    private static final String url = "http://api.raataar.de";
    private ProgressDialog pDialog;
    private String kategorie_id;
    private String searchtext;
    private String customTitle;
    private String Anzahl;

    ImageLoader imageLoader = AppController.getInstance().getImageLoader();

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

        Intent intent= getIntent();
        Bundle bundlevalues = intent.getExtras();

        this.setTitle("Suchen");
        if(bundlevalues!=null)
        {

            kategorie_id = (String) bundlevalues.get("kategorie_id");
            searchtext = (String) bundlevalues.get("searchtext");
            customTitle=(String) bundlevalues.get("titel");


        }


        // changing action bar color
        getActionBar().setBackgroundDrawable(
                new ColorDrawable(Color.parseColor("#1b1b1b")));


        ActionBar actionBar = getActionBar();
        actionBar.setDisplayHomeAsUpEnabled(true);


    }



    public void searchNow(View view) {
        // Do something in response to button
        EditText editText = (EditText) findViewById(R.id.searchtext);

        String searchText;
        searchText=editText.getText().toString();
        searchText=searchText.replaceAll("ä", "#ae#");
        searchText=searchText.replaceAll("Ä", "#Ae#");
        searchText=searchText.replaceAll("ü","#ue#");
        searchText=searchText.replaceAll("Ü","#Ue#");
        searchText=searchText.replaceAll("Ö","#Oe#");
        searchText=searchText.replaceAll("ö","#oe#");
        searchText=searchText.replaceAll("ß","#ss#");
        startMenueActivity("SuchenByIDActivity", searchText);
        //Toast.makeText(SuchenActivity.this, "Nicht gefunden Activity ", Toast.LENGTH_SHORT).show();

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        hidePDialog();
    }

    public void startMenueActivity(String MyClass,String searchtext){

        try {
            Intent openNewIntent = new Intent( this, Class.forName( AppController.getInstance().getPackageURL() + MyClass ) );
            openNewIntent.putExtra("searchtext", searchtext);
            startActivity(  openNewIntent );
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }


    private void hidePDialog() {
        if (pDialog != null) {
            pDialog.dismiss();
            pDialog = null;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.search_menu, menu);
        return true;
    }

    /** FIX sorgt dafür das Icons im Menu dargestellt werden **/

    @Override
    public boolean onMenuOpened(int featureId, Menu menu)
    {
        if(featureId == Window.FEATURE_ACTION_BAR && menu != null){
            if(menu.getClass().getSimpleName().equals("MenuBuilder")){
                try{
                    Method m = menu.getClass().getDeclaredMethod(
                            "setOptionalIconsVisible", Boolean.TYPE);
                    m.setAccessible(true);
                    m.invoke(menu, true);
                }
                catch(NoSuchMethodException e){
                    Log.e(TAG, "onMenuOpened", e);
                }
                catch(Exception e){
                    throw new RuntimeException(e);
                }
            }
        }
        return super.onMenuOpened(featureId, menu);
    }

    public void startSubMenueActivity(String MyClass){

        try {
            Intent openNewIntent = new Intent( this, Class.forName( AppController.getInstance().getPackageURL() + MyClass ) );
            startActivity(openNewIntent );
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_quit:
                moveTaskToBack(true);
                finish();
                return true;
            case R.id.action_kategorie:
                startSubMenueActivity("KategorieActivity");
                return true;
            case R.id.action_autoren:
                startSubMenueActivity("AutorenActivity");
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

}
