package com.zoulf.jianliao.frags.media;


import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.StyleRes;
import android.support.design.widget.BottomSheetDialog;
import android.support.design.widget.BottomSheetDialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import com.zoulf.common.tools.UITool;
import com.zoulf.common.widget.GalleryView;
import com.zoulf.common.widget.GalleryView.SelectedChangeListener;
import com.zoulf.jianliao.R;

/**
 * 图片选择Fragment
 */
public class GalleryFragment extends BottomSheetDialogFragment
    implements SelectedChangeListener {

  private GalleryView mGallery;
  private OnSelectedListener mListener;

  public GalleryFragment() {

  }

  @NonNull
  @Override
  public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new TransStatusBottomSheetDialog(getContext());
  }

  @Nullable
  @Override
  public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
      @Nullable Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.fragment_gallery, container, false);
    mGallery = (GalleryView) root.findViewById(R.id.galleryView);
    return root;
  }

  @Override
  public void onStart() {
    super.onStart();
    mGallery.setup(getLoaderManager(), this);
  }

  @Override
  public void onSelectedCountChanged(int count) {
    // 如果选中一张图片
    if (count > 0) {
      // 将Dialog隐藏
      dismiss();
      if (mListener != null) {
        // 得到所有的选中的 图片的路径
        String[] paths = mGallery.getSelectedPath();
        // 返回第一张
        mListener.onSelectedImage(paths[0]);
        // 取消和唤起者之间的引用，加快内存回收
        mListener = null;
      }
    }
  }

  /**
   * 设置事件监听，并返回自己
   *
   * @param listener OnSelectedListener
   * @return GalleryFragment
   */
  public GalleryFragment setOnSelectedListener(OnSelectedListener listener) {
    mListener = listener;
    return this;
  }

  /**
   * 选中图片的监听器
   */
  public interface OnSelectedListener {

    void onSelectedImage(String path);
  }

  /**
   * 为了顶部状态栏变透明而写的
   */
  public static class TransStatusBottomSheetDialog extends BottomSheetDialog {

    public TransStatusBottomSheetDialog(@NonNull Context context) {
      super(context);
    }

    public TransStatusBottomSheetDialog(@NonNull Context context,
        @StyleRes int theme) {
      super(context, theme);
    }

    protected TransStatusBottomSheetDialog(@NonNull Context context, boolean cancelable,
        OnCancelListener cancelListener) {
      super(context, cancelable, cancelListener);
    }

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

      final Window window = getWindow();
      if (window == null) {
        return;
      }

      //得到屏幕的高度
      int screenHeight = UITool.getScreenHeight(getOwnerActivity());

      // 得到状态栏的高度
      int statusHeight = UITool.getStatusBarHeight(getOwnerActivity());

      // dialog 的高度
      int dialogHeight = screenHeight - statusHeight;

      window.setLayout(LayoutParams.MATCH_PARENT,
          dialogHeight <= 0 ? LayoutParams.MATCH_PARENT : dialogHeight);
    }
  }
}
