package com.gh.base;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.gh.common.constant.Config;
import com.gh.common.util.DataUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.ConcernActivity;
import com.gh.gamecenter.DownloadManagerActivity;
import com.gh.gamecenter.MainActivity;
import com.gh.gamecenter.R;
import com.gh.gamecenter.SearchActivity;
import com.gh.gamecenter.eventbus.EBDownloadStatus;
import com.gh.gamecenter.eventbus.EBReuse;
import com.gh.gamecenter.eventbus.EBTopStatus;
import com.gh.gamecenter.manager.DataCollectionManager;
import com.gh.gamecenter.manager.PackageManager;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by LGT on 2016/9/9.
 */
public class HomeFragment extends Fragment implements View.OnClickListener {

	protected View view;
	protected LinearLayout home_ll_top;
	protected View home_slide_line;
	protected ViewPager home_vp_content;

	protected LinearLayout.LayoutParams lparams;

	protected Handler handler = new Handler();

	private TextView downloadHint;
	private TextView searchHint;

	private String hint;

	@Override
	public void onCreate(@Nullable Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		view = View.inflate(getActivity(), R.layout.fragment_home, null);

		SharedPreferences sp = getActivity().getSharedPreferences(
				Config.PREFERENCE, Context.MODE_PRIVATE);
		LinearLayout home_actionbar = (LinearLayout) view.findViewById(R.id.home_actionbar);
		LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, sp.getInt("actionbar_height",
				DisplayUtils.dip2px(getActivity(), 48)));

		if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
			int top = DisplayUtils.getInternalDimensionSize(getResources(),
					"status_bar_height");
			home_actionbar.setPadding(0, top, 0, 0);
			lparams.height += top;
		}
		home_actionbar.setLayoutParams(lparams);

		initActionBar();

		home_ll_top = (LinearLayout) view.findViewById(R.id.home_ll_top);
		home_slide_line = view.findViewById(R.id.home_slide_line);
		home_vp_content = (ViewPager) view.findViewById(R.id.home_vp_content);

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

	@Nullable
	@Override
	public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
	                         @Nullable Bundle savedInstanceState) {
		if (container != null) {
			container.removeView(view);
		}
		return view;
	}

	private void initActionBar() {
		view.findViewById(R.id.actionbar_rl_download).setOnClickListener(this);
		view.findViewById(R.id.actionbar_iv_search).setOnClickListener(this);
		view.findViewById(R.id.actionbar_notification).setOnClickListener(this);

		if (Config.isShow(getActivity())) {
			view.findViewById(R.id.actionbar_rl_download).setVisibility(View.VISIBLE);
		} else {
			view.findViewById(R.id.actionbar_rl_download).setVisibility(View.GONE);
		}

		downloadHint = (TextView) view.findViewById(R.id.action_tip);
		int updateSize = PackageManager.getUpdateListSize();
		int downloadSize = DownloadManager.getInstance(getActivity()).getAll().size();
		if (downloadSize != 0) {
			downloadHint.setVisibility(View.VISIBLE);
			downloadHint.setText(String.valueOf(downloadSize));
		} else if (updateSize != 0) {
			downloadHint.setVisibility(View.VISIBLE);
			downloadHint.setText(String.valueOf(updateSize));
		} else {
			downloadHint.setVisibility(View.GONE);
		}

		searchHint = (TextView) view.findViewById(R.id.actionbar_search_input);
		searchHint.setOnClickListener(this);
		if (!TextUtils.isEmpty(MainActivity.searchHint)) {
			hint = MainActivity.searchHint;
			searchHint.setHint(hint);
		}
	}

	@Override
	public void onClick(View v) {
		final int id = v.getId();
		if (id == R.id.actionbar_notification) {
			DataUtils.onEvent(getActivity(), "主页", "关注图标");

			Map<String, Object> map = new HashMap<>();
			map.put("location", "关注图标");
			map.put("page", "主页");
			DataCollectionManager.onEvent(getActivity(), "click-item", map);

			startActivity(new Intent(getActivity(), ConcernActivity.class));
		} else if (id == R.id.actionbar_rl_download) {
			DataUtils.onEvent(getActivity(), "主页", "下载图标");

			Map<String, Object> map = new HashMap<>();
			map.put("location", "下载图标");
			map.put("page", "主页");
			DataCollectionManager.onEvent(getActivity(), "click-item", map);

			startActivity(new Intent(getActivity(), DownloadManagerActivity.class));
		} else if (id == R.id.actionbar_search_input) {
			DataUtils.onEvent(getActivity(), "主页", "搜索框");

			Map<String, Object> map = new HashMap<>();
			map.put("location", "搜索框");
			map.put("page", "主页");
			DataCollectionManager.onEvent(getActivity(), "click-item", map);

			Intent goSearch = new Intent(getActivity(), SearchActivity.class);
			goSearch.putExtra("clicked", false);
			goSearch.putExtra("hint", hint);
			startActivity(goSearch);
		} else if (id == R.id.actionbar_iv_search) {
			DataUtils.onEvent(getActivity(), "主页", "搜索图标");

			Map<String, Object> map = new HashMap<>();
			map.put("location", "搜索图标");
			map.put("page", "主页");
			DataCollectionManager.onEvent(getActivity(), "click-item", map);

			Intent searchIntent = new Intent(getActivity(), SearchActivity.class);
			searchIntent.putExtra("clicked", true);
			searchIntent.putExtra("hint", hint);
			startActivity(searchIntent);
		}
	}

	// 打开下载按钮事件
	public void onEventMainThread(EBReuse reuse) {
		if ("Refresh".equals(reuse.getType())) {
			view.findViewById(R.id.actionbar_rl_download).setVisibility(View.VISIBLE);
		}
	}

	public void onEventMainThread(EBTopStatus status) {
		if (!TextUtils.isEmpty(status.getSearchHint())) {
			hint = status.getSearchHint();
			searchHint.setHint(hint);
		}
	}

	public void onEventMainThread(EBDownloadStatus status) {
		downloadHint = (TextView) view.findViewById(R.id.action_tip);
		int updateSize = PackageManager.getUpdateListSize();
		int downloadSize = DownloadManager.getInstance(getActivity()).getAll().size();
		if (downloadSize != 0) {
			downloadHint.setVisibility(View.VISIBLE);
			downloadHint.setText(String.valueOf(downloadSize));
		} else if (updateSize != 0) {
			downloadHint.setVisibility(View.VISIBLE);
			downloadHint.setText(String.valueOf(updateSize));
		} else {
			downloadHint.setVisibility(View.GONE);
		}
	}

	@Override
	public void onHiddenChanged(boolean hidden) {
		super.onHiddenChanged(hidden);
		FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
		List<Fragment> list = getChildFragmentManager().getFragments();
		if (list != null) {
			if (hidden) {
				for (Fragment fragment : getChildFragmentManager().getFragments()) {
					transaction.hide(fragment);
				}
			} else {
				for (Fragment fragment : getChildFragmentManager().getFragments()) {
					transaction.show(fragment);
				}
			}
		}
		transaction.commit();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		EventBus.getDefault().unregister(this);
		view = null;
		home_ll_top = null;
		home_slide_line = null;
		home_vp_content = null;
		lparams = null;
		handler = null;
		downloadHint = null;
		searchHint = null;
		hint = null;
	}
}
