package org.stepic.droid.ui.activities;

import android.app.SearchManager;
import android.app.SearchableInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import org.stepic.droid.R;
import org.stepic.droid.base.FragmentActivityBase;
import org.stepic.droid.ui.fragments.CourseSearchFragment;

import butterknife.ButterKnife;

public class CourseSearchResultActivity extends FragmentActivityBase {

    private String query;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitle(R.string.search_title);
        setContentView(R.layout.activity_search_courses);
        unbinder = ButterKnife.bind(this);
        query = getIntent().getStringExtra(SearchManager.QUERY);
        initOrTryRestoreFragment();
    }

    private void initOrTryRestoreFragment() {
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = fm.findFragmentById(R.id.frame);

        if (fragment == null) {
            fragment = createFragment();
            fm.beginTransaction()
                    .add(R.id.frame, fragment)
                    .commit();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        //add new fragment
        setIntent(intent);
        query = intent.getStringExtra(SearchManager.QUERY);
        FragmentManager fm = getSupportFragmentManager();
        Fragment fragment = createFragment();
        fm.beginTransaction()
                .replace(R.id.frame, fragment)
                .commit();
    }

    private Fragment createFragment() {
        return CourseSearchFragment.Companion.newInstance(query);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                // Respond to the action bar's Up/Home button
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu, menu);
        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        MenuItem menuItem = menu.findItem(R.id.action_search);
        SearchView searchView = (SearchView) menuItem.getActionView();

        ImageView closeImageView = searchView.findViewById(R.id.search_close_btn);
        closeImageView.setImageDrawable(ContextCompat.getDrawable(this, getCloseIconDrawableRes()));

        ComponentName componentName = getComponentName();
        SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);
        searchView.setSearchableInfo(searchableInfo);
        searchView.setMaxWidth(20000);//it is dirty hack for expand in landscape
        menuItem.expandActionView();
        if (query != null) {
            searchView.setQuery(query, false);
        }
        searchView.clearFocus();

        menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() {
            @Override
            public boolean onMenuItemActionExpand(MenuItem item) {
                return true;
            }

            @Override
            public boolean onMenuItemActionCollapse(MenuItem item) {
                finish();
                return true;
            }
        });
        return true;
    }

    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_from_start, R.anim.slide_out_to_end);
    }

}

@@ -72,113 +72,8 @@
 er;%0A
-import android.app.SearchableInfo;%0Aimport android.content.ComponentName;%0Aimport android.content.Context;%0A
 impo
@@ -231,174 +231,21 @@
 oid.
-support.v4.content.ContextCompat;%0Aimport android.support.v7.widget.SearchView;%0Aimport android.view.Menu;%0Aimport android.view.MenuItem;%0Aimport android.widget.ImageView
+view.MenuItem
 ;%0A%0Ai
@@ -2082,1405 +2082,8 @@
  %7D%0A%0A
-    @Override%0A    public boolean onCreateOptionsMenu(Menu menu) %7B%0A        getMenuInflater().inflate(R.menu.search_menu, menu);%0A        SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);%0A        MenuItem menuItem = menu.findItem(R.id.action_search);%0A        SearchView searchView = (SearchView) menuItem.getActionView();%0A%0A        ImageView closeImageView = searchView.findViewById(R.id.search_close_btn);%0A        closeImageView.setImageDrawable(ContextCompat.getDrawable(this, getCloseIconDrawableRes()));%0A%0A        ComponentName componentName = getComponentName();%0A        SearchableInfo searchableInfo = searchManager.getSearchableInfo(componentName);%0A        searchView.setSearchableInfo(searchableInfo);%0A        searchView.setMaxWidth(20000);//it is dirty hack for expand in landscape%0A        menuItem.expandActionView();%0A        if (query != null) %7B%0A            searchView.setQuery(query, false);%0A        %7D%0A        searchView.clearFocus();%0A%0A        menuItem.setOnActionExpandListener(new MenuItem.OnActionExpandListener() %7B%0A            @Override%0A            public boolean onMenuItemActionExpand(MenuItem item) %7B%0A                return true;%0A            %7D%0A%0A            @Override%0A            public boolean onMenuItemActionCollapse(MenuItem item) %7B%0A                finish();%0A                return true;%0A            %7D%0A        %7D);%0A        return true;%0A    %7D%0A%0A
     

fix duplicate search menu due to merge mistake