package com.abbidot.tracker.ui.common

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.os.Parcelable
import androidx.activity.result.contract.ActivityResultContract
import com.abbidot.tracker.constant.ConstantString
import com.abbidot.tracker.ui.activity.MyCropImageActivity
import com.canhub.cropper.CropImage
import com.canhub.cropper.CropImageView
import com.huantansheng.easyphotos.models.album.entity.Photo

/**
 *Created  by .yzq on 2022/4/24/024.
 * @link
 * @description:用于接收跳转裁剪图片的页面
 */
class MyCropImageContract(aspectRatioX: Int, aspectRatioY: Int) :
    ActivityResultContract<Photo, CropImageView.CropResult>() {

    private val mAspectRatioX = aspectRatioX
    private val mAspectRatioY = aspectRatioY

    override fun createIntent(context: Context, input: Photo): Intent {
        return Intent(context, MyCropImageActivity::class.java).apply {
            putExtra(ConstantString.Photo, input)
            putExtra(ConstantString.Type, mAspectRatioX)
            putExtra(ConstantString.Index, mAspectRatioY)
        }
    }

    override fun parseResult(resultCode: Int, intent: Intent?): CropImageView.CropResult {
        val result = intent?.getParcelableExtra<Parcelable>(
            CropImage.CROP_IMAGE_EXTRA_RESULT
        ) as? CropImage.ActivityResult?

        return if (result == null || resultCode == Activity.RESULT_CANCELED) CropImage.CancelledResult
        else result
    }
}