package com.usai.apex;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.TextView;

import com.usai.util.dbUtil;

public class ContainerSearchActivity extends AppCompatActivity {


    private SegmentView mSegmentView;
    private AutoCompleteTextView searchField;
    private Button cancelBtn;

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

        cancelBtn = findViewById(R.id.button2);
        searchField = findViewById(R.id.atv_criterion2);

        cancelBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                searchContainer();

                searchField.clearFocus();
                InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                inputMethodManager.hideSoftInputFromWindow(searchField.getWindowToken(),0);
                inputMethodManager.hideStatusIcon(searchField.getWindowToken());
            }
        });

        searchField.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent event) {

                // 当actionId == XX_SEND 或者 XX_DONE时都触发
                // 或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
                // 注意，这是一定要判断event != null。因为在某些输入法上会返回null。
                if (i == EditorInfo.IME_ACTION_SEND || i == EditorInfo.IME_ACTION_DONE || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
                    searchContainer();
                }

                return false;
            }
        });

        setCustomActionBar();
    }

    private void setCustomActionBar() {
        ActionBar.LayoutParams lp =new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT, Gravity.CENTER);
        View mActionBarVie = LayoutInflater.from(this).inflate(R.layout.actionbar_segmentview, null);


        mSegmentView = mActionBarVie.findViewById(R.id.segmentview);

//
//        mSegmentView.setOnSegmentViewClickListener(new SegmentView.onSegmentViewClickListener() {
//            @Override
//            public void onSegmentViewClick(View view, int postion) {
//                switch (postion) {
//                    case 0:
//                        Toast.makeText(ContainerSearchActivity.this, "点击了消息" + postion,
//                                Toast.LENGTH_SHORT).show();
//                        break;
//                    case 1:
//                        Toast.makeText(ContainerSearchActivity.this, "点击了电话" + postion,
//                                Toast.LENGTH_SHORT).show();
//                        break;
//                    default:
//                        break;
//                }
//            }
//        });


//        TextView titleview = mActionBarView.findViewById(R.id.title);
//        titleview.setText(getIntent().getStringExtra("title"));
//
//        mActionBarView.setBackgroundColor(Color.YELLOW);
//        titleview.setBackgroundColor(Color.BLUE);
        ActionBar actionBar = getSupportActionBar();
        actionBar.setCustomView(mActionBarVie, lp);
        actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
        actionBar.setDisplayShowCustomEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);


//        actionBar.setIcon(getNumberDrawable());
//        actionBar.setDisplayShowHomeEnabled(true);
        actionBar.setDisplayShowTitleEnabled(false);
    }



    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        Intent intent = new Intent();
        switch (item.getItemId())
        {
            case android.R.id.home:
                finish();
                break;

            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    private void searchContainer() {

        InputMethodManager inputMethodManager = (InputMethodManager) getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(searchField.getWindowToken(),0);
        inputMethodManager.hideStatusIcon(searchField.getWindowToken());

        String cargo_criterion = searchField.getText().toString();

        Intent intent = new Intent();
        intent.setClass(this, DetailActivity.class);

        intent.putExtra("action0", "Tracking");
        intent.putExtra("function_name", "Cargo Tracking");

        intent.putExtra("cargo_criterion", cargo_criterion);
        intent.putExtra("actions_count", 1);
        intent.putExtra("_id", "dumb");
        String h_field;

        int selectedId = mSegmentView.getSelect();
        if (selectedId == 0)
        {
            intent.putExtra("criterion_type", 0);
            h_field = "h_bol";
        }
        else
        {
            intent.putExtra("criterion_type", 1);
            h_field = "ctnr";
        }

        dbUtil.savehistory(h_field, cargo_criterion);

        startActivity(intent);
    }

}
