package com.example.cmknowledgegraph;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatEditText;
import androidx.fragment.app.Fragment;

import com.example.PersonalCenter.Search;
import com.hankcs.hanlp.seg.common.Term;
import com.hankcs.hanlp.tokenizer.StandardTokenizer;
import com.yalantis.jellytoolbar.listener.JellyListener;
import com.yalantis.jellytoolbar.widget.JellyToolbar;

import java.util.List;
import java.util.Set;
//
public class SearchContent extends Fragment {
    private static final String TEXT_KEY = "text";
    AppCompatActivity appCompatActivity;

    public AppCompatActivity getAppCompatActivity() {
        return appCompatActivity;
    }

    public void setAppCompatActivity(AppCompatActivity appCompatActivity) {
        this.appCompatActivity = appCompatActivity;
    }
    private JellyToolbar toolbar;
    private AppCompatEditText editText;
    private JellyListener jellyListener = new JellyListener() {
        @Override
        public void onCancelIconClicked() {
            if (TextUtils.isEmpty(editText.getText())) {
                toolbar.collapse();
            } else {
                editText.getText().clear();
            }
        }
    };
    public static List<Set<String>> a;
    public static int k=0;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate( R.layout.activity_search, container, false );  //要加载的layout文件
    }


    private int getStatusBarHeight() {
        int result = 0;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            result = getResources().getDimensionPixelSize(resourceId);
        }
        return result;
    }

    @Override
    public void onSaveInstanceState(@NonNull Bundle outState) {
        outState.putString(TEXT_KEY, editText.getText().toString());
        super.onSaveInstanceState(outState);
    }

//
//    @Override
//    public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
//        super.onViewStateRestored(savedInstanceState);
//
//        editText.setText(savedInstanceState.getString(TEXT_KEY));
//        editText.setSelection(editText.getText().length());
//    }

    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        toolbar = (JellyToolbar) appCompatActivity.findViewById(R.id.toolbar);
        toolbar.getToolbar().setNavigationIcon(R.drawable.ic_menu);
        toolbar.setJellyListener(jellyListener);
        toolbar.getToolbar().setPadding(0, getStatusBarHeight(), 0, 0);

        editText = (AppCompatEditText) LayoutInflater.from(appCompatActivity).inflate(R.layout.edit_text, null);
        editText.setBackgroundResource(R.color.colorTransparent);

        toolbar.setContentView(editText);//添加入输入框

        appCompatActivity.getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        toolbar.setJellyListener(jellyListener);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if((event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())){
                    Log.i("dianjile","你点击了哥哥");
                    toolbar.collapse();
                    editText.getText().clear();
                    return true;
                }
                return false;
            }
        });



            // TODO Auto-generated method stub
//         	super.onActivityCreated(savedInstanceState);
//        EditText search_edit = getActivity().findViewById(R.id.search_edit);
//
//         	ImageButton search_btn = (ImageButton) getActivity().findViewById(R.id.search_btn);
//
//            search_btn.setOnClickListener(new View.OnClickListener() {
//         	        public void onClick(View v) {
//                        Log.i("haha","=======Oncli");
//         	            //查找逻辑
//                        List<Term> termList = StandardTokenizer.segment("什么是糖尿病");
//                        System.out.println(termList);
//                        String sickname = search_edit.getText().toString();
//                        Search search = new Search(sickname);
//                        new Thread(new Runnable() {
//                            @Override
//                            public void run() {
//                                 SearchContent.a = search.search();
//                                 SearchContent.k = 1;
//
//                            }
//                        }).start();
//                        while(SearchContent.k==0) {Log.i("i","i");}
//                        if(SearchContent.a!=null){
//                            String[] medicines = a.get(0).toArray(new String[0]);
//                            String[] reasons = a.get(1).toArray(new String[0]);
//                            ListView lv = getActivity().findViewById(R.id.listView1);
//                            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1,medicines);
//                            lv.setAdapter(adapter);
//
//                        }
//                        SearchContent.k=0;
//                        SearchContent.a=null;
//
//
//                }
//            });
    }
}
