package org.edx.mobile.view;

import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import org.edx.mobile.R;
import org.edx.mobile.databinding.FragmentWebviewCourseDiscoveryBinding;
import org.edx.mobile.event.MainDashboardRefreshEvent;
import org.edx.mobile.event.NetworkConnectivityChangeEvent;
import org.edx.mobile.logger.Logger;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import de.greenrobot.event.EventBus;

public class WebViewDiscoverCoursesFragment extends BaseWebViewDiscoverFragment {
    private FragmentWebviewCourseDiscoveryBinding binding;
    private SearchView searchView;
    private MainDashboardToolbarCallbacks toolbarCallbacks;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_webview_course_discovery, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        loadUrl(getInitialUrl());
        EventBus.getDefault().register(this);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        toolbarCallbacks = (MainDashboardToolbarCallbacks) getActivity();
        initSearchView();

    }

    private void initSearchView() {
        searchView = toolbarCallbacks.getSearchView();
        searchView.setQueryHint(getResources().getString(R.string.search_for_courses));
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                String baseUrl = environment.getConfig().getCourseDiscoveryConfig().getCourseSearchUrl();
                String searchUrl = buildQuery(baseUrl, query, logger);
                searchView.onActionViewCollapsed();
                loadUrl(searchUrl);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
        searchView.setOnQueryTextFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean queryTextFocused) {
                if (!queryTextFocused) {
                    toolbarCallbacks.getTitleView().setVisibility(View.VISIBLE);
                    searchView.onActionViewCollapsed();
                } else {
                    toolbarCallbacks.getTitleView().setVisibility(View.GONE);
                }
            }
        });
    }

    @NonNull
    protected String getInitialUrl() {
        return environment.getConfig().getCourseDiscoveryConfig().getCourseSearchUrl();
    }

    public static String buildQuery(@NonNull String baseUrl, @NonNull String query, @NonNull Logger logger) {
        String encodedQuery = null;
        try {
            encodedQuery = URLEncoder.encode(query, "UTF-8");
        } catch (UnsupportedEncodingException e) {
            logger.error(e);
        }
        String searchTerm = "search_query=" + encodedQuery;

        String searchUrl;
        if (baseUrl.contains("?")) {
            searchUrl = baseUrl + "&" + searchTerm;
        } else {
            searchUrl = baseUrl + "?" + searchTerm;
        }
        return searchUrl;
    }

    @SuppressWarnings("unused")
    public void onEvent(MainDashboardRefreshEvent event) {
        loadUrl(getInitialUrl());
    }

    @Override
    public void onRefresh() {
        EventBus.getDefault().post(new MainDashboardRefreshEvent());
    }

    @SuppressWarnings("unused")
    public void onEvent(NetworkConnectivityChangeEvent event) {
        onNetworkConnectivityChangeEvent(event);
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (searchView != null) {
            searchView.setVisibility(isVisibleToUser ? View.VISIBLE : View.GONE);
            searchView.setIconified(!isVisibleToUser);
        }
    }

    @Override
    protected boolean isShowingFullScreenError() {
        return errorNotification != null && errorNotification.isShowing();
    }
}

@@ -27,40 +27,8 @@
 w;%0A%0A
-import android.content.Context;%0A
 impo
@@ -1491,63 +1491,112 @@
  onA
-ttach(Context context) %7B%0A        super.onAttach(context
+ctivityCreated(@Nullable Bundle savedInstanceState) %7B%0A        super.onActivityCreated(savedInstanceState
 );%0A 
@@ -1664,24 +1664,24 @@
 Activity();%0A
+
         init
@@ -1694,17 +1694,16 @@
 View();%0A
-%0A
     %7D%0A%0A 

Move initialization of SearchView from onAttach to onActivityCreated