package com.usai.redant.Search;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ExpandableListView;

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

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

import java.util.Iterator;

public class RamSearchActivity extends CommonEditorActivity {

    private Context mContext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        mContext = this;

        hasChildView = true;
        setContentView(R.layout.activity_ram_search);
        listView = (ExpandableListView)findViewById(R.id.ram_search_list_view);

        super.onCreate(savedInstanceState);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.ram_search_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if (item.getItemId() == R.id.ram_search_search_btn) {
            JSONObject upparams = check_cancomit(false);

            if (cancommit == false) {
                return true;
            }

            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);
                        }
                    }
                }

                JSONArray criteria = RAUtil.jsonObject2KVArray(upparams,true);

                Bundle result_params = new Bundle();
                result_params.putString("criteria",criteria.toString());
                result_params.putString("query_id",selfParams.getString("query_id"));
                result_params.putString("title",selfParams.getString("title"));

                Intent intent = new Intent(mContext, SearchResultActivity.class);
                intent.putExtra("query_params",result_params);

                startActivity(intent);

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


        }

        return true;
    }

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

        params.putString("_action","ant_mobile");
        params.putString("_operate","getCondition");

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

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

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

            }
        });
        return ret;
    }
}
