package com.ifavine.pay.utils;

import android.net.Uri;
import android.os.Environment;


import com.jph.takephoto.app.TakePhoto;
import com.jph.takephoto.compress.CompressConfig;
import com.jph.takephoto.model.CropOptions;
import com.jph.takephoto.model.LubanOptions;
import com.jph.takephoto.model.TakePhotoOptions;

import java.io.File;

public class CustomTakePhotoHelper {


    public static CustomTakePhotoHelper of() {
        return new CustomTakePhotoHelper();
    }

    private CustomTakePhotoHelper() {
    }

    private void init(TakePhoto takePhoto) {
        configCompress(takePhoto);
        configTakePhotoOption(takePhoto);
    }

    public void pickBySelect(TakePhoto takePhoto){
        init(takePhoto);
        takePhoto.onPickFromGallery();
//        takePhoto.onPickMultiple(8);
    }

    public void pickByTake(TakePhoto takePhoto){
        File file = new File(Environment.getExternalStorageDirectory(),
            "/temp/" + System.currentTimeMillis() + ".jpg");
        if (!file.getParentFile().exists()) file.getParentFile().mkdirs();
        Uri imageUri = Uri.fromFile(file);
        init(takePhoto);
        takePhoto.onPickFromCapture(imageUri);
    }


    private void configTakePhotoOption(TakePhoto takePhoto) {
        TakePhotoOptions.Builder builder = new TakePhotoOptions.Builder();
        if (true) {
            builder.setWithOwnGallery(true);
        }else{
            builder.setCorrectImage(true);
        }
        takePhoto.setTakePhotoOptions(builder.create());
    }

    private void configCompress(TakePhoto takePhoto) {
        //if (rgCompress.getCheckedRadioButtonId() != R.id.rbCompressYes) {
        //    takePhoto.onEnableCompress(null, false);
        //    return;
        //}
        int maxSize = Integer.parseInt("102400");
        int width = Integer.parseInt("800");
        int height = Integer.parseInt("800");

        CompressConfig config;
        if (true) {
            config = new CompressConfig.Builder().setMaxSize(maxSize)
                .setMaxPixel(width >= height ? width : height)
                .enableReserveRaw(true)
                .create();
        } else {
            LubanOptions option = new LubanOptions.Builder().setMaxHeight(height)
                .setMaxWidth(width)
                .setMaxSize(maxSize)
                .create();
            config = CompressConfig.ofLuban(option);
            config.enableReserveRaw(true);
        }
        takePhoto.onEnableCompress(config, true);
    }

    private CropOptions getCropOptions() {
        int height = Integer.parseInt("800");
        int width = Integer.parseInt("800");

        CropOptions.Builder builder = new CropOptions.Builder();

        builder.setAspectX(width).setAspectY(height);
        builder.setOutputX(width).setOutputY(height);
        builder.setWithOwnCrop(true);
        return builder.create();
    }
}
