package kr.co.zumo.app.lifeplus.manager;

import android.app.Activity;
import android.support.annotation.StringRes;
import android.support.v7.app.ActionBar;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import kr.co.zumo.app.R;

/**
 * Fragment의 ActionBar 관리
 * <pre>
 * </pre>
 *
 * @author 하세미
 * @version 1.0
 * @history 하세미   [2018. 9. 5.]   [최초 작성]
 * @since 2018. 9. 5.
 */
public class ActionBarManager implements IActionBarListener {

  private static final ActionBarManager ourInstance = new ActionBarManager();

  public static ActionBarManager getInstance() {
    return ourInstance;
  }

  private ActionBar actionBar;
  private TextView actionBarTitle;
  private View imageBi;
  private View buttonBack;
  private View buttonSearch;
  private View buttonMenu;
  private ProgressBar progressBar;

  private IActionBarListener listener;

  private ActionBarManager() {
  }

  /**
   * Action bar event listener
   *
   * @param listener
   */
  public void setListener(IActionBarListener listener) {
    this.listener = listener;
  }

  /**
   * actionBar 정보 받아옴.
   *
   * @param actionBar
   */
  public void init(Activity activity, ActionBar actionBar) {
    this.actionBar = actionBar;

    LayoutInflater inflater = LayoutInflater.from(activity);
    View view = inflater.inflate(R.layout.action_bar, null);

    actionBarTitle = view.findViewById(R.id.action_bar_title);
    progressBar = view.findViewById(R.id.progress_bar);
    imageBi = view.findViewById(R.id.image_bi);
    buttonSearch = view.findViewById(R.id.button_search);
    buttonMenu = view.findViewById(R.id.button_menu);

    buttonBack = view.findViewById(R.id.button_back);
    buttonBack.setOnClickListener(v -> {
      onClickBack(actionBar);
    });
    buttonSearch.setOnClickListener(v -> {
      onClickSearch(actionBar);
    });
    buttonMenu.setOnClickListener(v -> {
      onClickMenu(actionBar);
    });

    setCustomView(view);
  }

  public void setTitleVisible(boolean isVisible) {
    setViewVisible(actionBarTitle, isVisible);
  }

  public void setBackVisible(boolean isVisible) {
    setViewVisible(buttonBack, isVisible);
  }

  public void setBiVisible(boolean isVisible) {
    setViewVisible(imageBi, isVisible);
  }

  public void setSearchVisible(boolean isVisible) {
    setViewVisible(buttonSearch, isVisible);
  }

  public void setMenuVisible(boolean isVisible) {
    setViewVisible(buttonMenu, isVisible);
  }

  private void setViewVisible(View view, boolean isVisible) {
    view.setVisibility(isVisible ? View.VISIBLE : View.INVISIBLE);
  }


  /**
   * actionbar 제목 설정
   *
   * @param stringId
   */
  public void setTitle(@StringRes int stringId) {
    actionBarTitle.setText(stringId);
  }

  /**
   * actionbar hide
   */
  public void hide() {
    actionBar.hide();
  }

  /**
   * show
   */
  public void show() {
    actionBar.show();
  }

  /**
   * Custom Layout 을 지정한다.
   *
   * @param view
   */
  private void setCustomView(View view) {

    // Custom Actionbar 를 사용하기 위해 CustomEnabled 을 true 시키고 필요 없는 것은 false 시킨다
    actionBar.setDisplayShowCustomEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(false);      //액션바 아이콘을 업 네비게이션 형태로 표시합니다.
    actionBar.setDisplayShowTitleEnabled(false);    //액션바에 표시되는 제목의 표시유무를 설정합니다.
    actionBar.setDisplayShowHomeEnabled(false);      //홈 아이콘을 숨김처리합니다.
    actionBar.setCustomView(view);

    Toolbar parent = (Toolbar) view.getParent();
    parent.setPadding(0, 0, 0, 0);//for tab otherwise give space in tab
    parent.setContentInsetsAbsolute(0, 0);
    parent.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT));

    setDisplayOptions(android.app.ActionBar.DISPLAY_SHOW_CUSTOM);
    setElevation(0);
  }

  /**
   * Progress bar 의 진행율 설정
   *
   * @param percent 0~100
   */
  public void setProgress(int percent) {
    View customView = actionBar.getCustomView();
    if (null != customView) {
      progressBar.setProgress(percent);
    }
  }

  /**
   * progress bar 보이기/안보이기
   *
   * @param isVisible
   */
  public void setProgressVisible(boolean isVisible) {
    setViewVisible(progressBar, isVisible);
  }

  private void setDisplayOptions(int displayOptions) {
    actionBar.setDisplayOptions(displayOptions);
  }

  private void setElevation(int elevation) {
    actionBar.setElevation(elevation);
  }

  @Override
  public void onClickBack(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickBack(actionBar);
    }
  }

  @Override
  public void onClickTitle(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickTitle(actionBar);
    }
  }

  @Override
  public void onClickSearch(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickSearch(actionBar);
    }
  }

  @Override
  public void onClickMenu(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickMenu(actionBar);
    }
  }

  @Override
  public void onClickLike(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickLike(actionBar);
    }
  }

  @Override
  public void onClickBookmark(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickBookmark(actionBar);
    }
  }

  @Override
  public void onClickShare(ActionBar actionBar) {
    if (null != listener) {
      listener.onClickShare(actionBar);
    }
  }

  /***********************************
   * life-cycle
   ***********************************/
  //  public void init() {
//
//  }
//
//  public void start() {
//
//  }
//
//  public void stop() {
//
//  }
//
//  public void dispose() {
//
//  }

}
