package com.usai.redant.Search;

import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;

import com.usai.redant.CommonEditor.CommonEditorActivity;
import com.usai.redant.redantmobile.R;
import com.usai.redant.util.Network;
import com.usai.redant.util.RAUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.Iterator;

public class RamSearchSettingActivity extends CommonEditorActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {


        hasChildView = true;
        setContentView(R.layout.activity_ram_search_setting);
        listView = (ExpandableListView)findViewById(R.id.ram_search_setting_list_view);

        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.ram_search_setting_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.ram_search_setting_save) {

            JSONObject upparams = check_cancomit(false);

            try {

                JSONObject hidden_params = content_data_download.optJSONObject("hidden_params");

                if (hidden_params != null) {

                    Iterator<String> iterator = hidden_params.keys();
                    while (iterator.hasNext()) {
                        String key = iterator.next();
                        Object value = hidden_params.get(key);
                        if (value != null) {
                            upparams.put(key,value);
                        }
                    }
                }

                upparams.put("query_id",selfParams.getString("query_id"));

                final Bundle bundle = RAUtil.Json2Bundle(upparams);

                new Thread(new Runnable() {
                    @Override
                    public void run() {

                        final JSONObject contentDic = Network.save_query_setting(bundle);

                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    if (contentDic == null) {
                                        showWarningAlert("Some error occurred");
                                    } else {
                                        int result = contentDic.getInt("result");
                                        if (result == 2) {
                                            finish();
                                        } else {
                                            String msg = contentDic.getString("msg");
                                            if (msg == null || msg.isEmpty()) {
                                                msg = "Some error occurred";
                                            }
                                            showWarningAlert(msg);
                                        }
                                    }

                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
                        });


                    }
                }).start();

            } catch (JSONException e) {
                e.printStackTrace();
            }

        }

        return true;
    }

    @Override
    public JSONObject request_editor(String request_url, Bundle params) {

        final JSONObject ret = Network.request_setting(request_url,params);

        runOnUiThread(new Runnable() {
            @Override
            public void run() {

                String title = ret.optString("table_title");
                setTitle(title);

            }
        });
        return ret;
    }
}
