package com.bc_chat.contacts.view;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;

import com.bc_chat.bc_base.databinding.DialogPictureSelectBinding;
import com.bc_chat.contacts.R;
import com.zhaohaoting.framework.abs.AbsBottomDialogFragment;
import com.zhaohaoting.framework.abs.contract.BaseContract;
import com.zhaohaoting.framework.imagepicker.ImagePicker;
import com.zhaohaoting.framework.imagepicker.bean.MediaItem;
import com.zhaohaoting.framework.imagepicker.ui.ImageGridActivity;
import com.zhaohaoting.framework.imagepicker.view.CropImageView;
import com.zhaohaoting.framework.utils.ActivityUtils;

import java.util.List;

/**
 * author：Created by zhaohaoting on 2019/2/22
 * email：526309416@qq.com
 * desc：获取图片对话框
 */
public class PictureDialog1Fragment extends AbsBottomDialogFragment<BaseContract.BasePresenter> implements View.OnClickListener {

    private static final int PICK_PHOTO = 603;//相册
    private static final int TAKE_PHOTO = 604;//拍照
    private boolean isCrop = false;
    private int width = 1280;//宽度
    private int height = 1280;//高度
    private int selectLimit;
    private int cropType; //1圆形 2 方形

    public static PictureDialog1Fragment newInstance(Bundle args) {
        PictureDialog1Fragment fragment = new PictureDialog1Fragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void initialize() {
        DialogPictureSelectBinding binding = (DialogPictureSelectBinding) rootDataBinding;
        binding.setOnClickEvent(this);
        if (getArguments() != null) {
            isCrop = getArguments().getBoolean("isCrop", false);
            width = getArguments().getInt("width", 1280);
            height = getArguments().getInt("height", 1280);
            cropType = getArguments().getInt("cropType", 1);
            selectLimit = getArguments().getInt("selectLimit", 1);
            ImagePicker.getInstance().setCrop(isCrop);
            if (selectLimit > 1) {
                ImagePicker.getInstance().setMultiMode(true);
                ImagePicker.getInstance().setSelectLimit(selectLimit);
            } else {
                ImagePicker.getInstance().setMultiMode(false);
            }
            ImagePicker.getInstance().setOutPutX(width);
            ImagePicker.getInstance().setOutPutY(height);
            ImagePicker.getInstance().setStyle(cropType == 2 ? CropImageView.Style.RECTANGLE : CropImageView.Style.CIRCLE);
        }
    }

    @Override
    public BaseContract.BasePresenter initPresenter() {
        return null;
    }

    @Override
    public int getLayoutResId() {
        return R.layout.dialog_picture_select;
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.tv_take_pictures) {
            ActivityUtils.newInstance(this).putExtra(ImageGridActivity.EXTRAS_TAKE_PICKERS, true).navigation(ImageGridActivity.class, TAKE_PHOTO);
        } else if (v.getId() == R.id.tv_album) {
            ActivityUtils.newInstance(this).navigation(ImageGridActivity.class, PICK_PHOTO);
        } else if (v.getId() == R.id.tv_cancel) {
            dismissAllowingStateLoss();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS) {
            switch (requestCode) {
                case PICK_PHOTO:
                case TAKE_PHOTO:
                    List<MediaItem> images = data.getParcelableArrayListExtra(ImagePicker.EXTRA_RESULT_ITEMS);
                    if (photoPathListener != null) {
                        photoPathListener.picturePhotoPath(images);
                        dismissAllowingStateLoss();
                    }
                    break;
            }
        }
    }

    public void setPicturePhotoPathListener(OnPicturePhotoPathListener photoPathListener) {
        this.photoPathListener = photoPathListener;
    }

    private OnPicturePhotoPathListener photoPathListener = null;

    public interface OnPicturePhotoPathListener {
        void picturePhotoPath(List<MediaItem> mediaItemList);
    }

}