package com.ericsson.uecontrol.gui.fragments;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.InputType;
import android.view.ContextThemeWrapper;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ericsson.uecontrol.R;
import com.ericsson.uecontrol.core.UeBehaviour;
import com.ericsson.uecontrol.gui.MainActivity;
import com.ericsson.uecontrol.core.util.Configurator;
import com.ericsson.uecontrol.core.util.Configurator.ConfigType;
import com.ericsson.uecontrol.core.util.Configurator.ConfigurationParam;

import java.util.HashMap;

/**
 * Created by ezivkoc on 2014-07-24.
 */
public class ConfigureDialog extends DialogFragment {
    private HashMap<String, Object> inputs;
    private Configurator confer;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        inputs = new HashMap<String, Object>();

        // Fix for CopyPaste bar not visible
        Context context = new ContextThemeWrapper(getActivity(), android.R.style.Theme_Holo_Light);

        // Create dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(context);
        builder.setTitle(R.string.configure_behaviour);

        LinearLayout root = new LinearLayout(context);
        root.setOrientation(LinearLayout.VERTICAL);
        root.setLayoutParams(new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT));
        int padding = getResources().getDimensionPixelSize(R.dimen.standard_padding);
        root.setPadding(padding, padding, padding, padding);

        UeBehaviour behaviour = MainActivity.getExecutor().getBehaviourList().get(this.getArguments().getInt("object_index"));
        confer = new Configurator(behaviour);
        for(ConfigurationParam confParam : confer.getConfiguration()){
            LinearLayout layout = new LinearLayout(context);
            layout.setOrientation(
                    (confParam.getNiceName().length() < 25 ? LinearLayout.HORIZONTAL : LinearLayout.VERTICAL));
            layout.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.MATCH_PARENT,
                    LinearLayout.LayoutParams.MATCH_PARENT));

            TextView label = new TextView(context);
            label.setText(confParam.getNiceName()+": ");
            label.setLayoutParams(new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT));
            layout.addView(label);
            if(confParam.getType() == ConfigType.STRING || confParam.getType() == ConfigType.INT) {
                EditText input = new EditText(context);
                input.setLayoutParams(new LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT));
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
                // Set previous value
                if (savedInstanceState != null && savedInstanceState.containsKey(confParam.getName()))
                    input.setText(savedInstanceState.getString(confParam.getName()), TextView.BufferType.EDITABLE);
                else
                    input.setText(confParam.getString(), TextView.BufferType.EDITABLE);

                // store inputs for later use
                inputs.put(confParam.getName(), input);
                layout.addView(input);
            }
            else if(confParam.getType() == ConfigType.BOOLEAN) {
                CheckBox input = new CheckBox(context);
                // Set previous value
                if (savedInstanceState != null && savedInstanceState.containsKey(confParam.getName()))
                    input.setChecked(Boolean.parseBoolean(savedInstanceState.getString(confParam.getName())));
                else
                    input.setChecked(Boolean.parseBoolean(confParam.getString()));
                // store inputs for later use
                inputs.put(confParam.getName(), input);
                layout.addView(input);
            }

            root.addView(layout);
        }


        builder.setPositiveButton(R.string.action_save, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                for (ConfigurationParam confParam : confer.getConfiguration()) {
                    if(inputs.get(confParam.getName()) instanceof EditText)
                        confParam.setString(((EditText)inputs.get(confParam.getName())).getText().toString());
                    else if(inputs.get(confParam.getName()) instanceof CheckBox)
                        confParam.setString(""+ ((CheckBox)inputs.get(confParam.getName())).isChecked());
                }
                confer.setConfiguration();
            }
        });
        builder.setNegativeButton(R.string.action_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int id) {
                ConfigureDialog.this.getDialog().cancel();
            }
        });

        builder.setView(root);
        return builder.create();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        for(String key : inputs.keySet()){
            if(inputs.get(key) instanceof EditText)
                outState.putString(key, ((EditText)inputs.get(key)).getText().toString());
            else if(inputs.get(key) instanceof CheckBox)
                outState.putString(key, ""+ ((CheckBox)inputs.get(key)).isChecked());
        }
    }
}
