<commit_before>package hypr.a255bits.com.hypr.ModelFragmnt

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import hypr.a255bits.com.hypr.Generator.Generator
import hypr.a255bits.com.hypr.GeneratorLoader.EasyGeneratorLoader
import hypr.a255bits.com.hypr.GeneratorLoader.FaceLocation
import hypr.a255bits.com.hypr.R
import hypr.a255bits.com.hypr.Util.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.coroutines.experimental.bg
import org.jetbrains.anko.toast
import java.io.File
import java.io.IOException
import kotlin.properties.Delegates


class ModelFragmentPresenter(val view: ModelFragmentMVP.view, val interactor: ModelInteractor, val context: Context, pbFile: File?) : ModelFragmentMVP.presenter {

    init {
        val cont = context
        launch(UI) {
            val imageBitmap = bg {
                loadGenerator(pbFile, cont.assets)
                if (byteArrayImage == null) {
                    byteArrayImage = changePixelToBitmap(easyGenerator.sampleImageWithoutImage())?.toByteArray()
                }
                val bitmap = byteArrayImage?.toBitmap()
                val faces = getFaceCroppedOutOfImageIfNoFaceGetFullImage(bitmap, cont)
                val transformedImage = sampleImage(faces)
                return@bg inlineImage(bitmap!!, transformedImage, fullImage)
            }
            view.displayFocusedImage(imageBitmap.await())
        }
    }

    val analytics = Analytics(context)
    var imageFromGallery: IntArray? = null
    val SHARE_IMAGE_PERMISSION_REQUEST = 10
    val SAVE_IMAGE_PERMISSION_REQUEST: Int = 11
    lateinit var easyGenerator: EasyGeneratorLoader
    var generator: Generator by Delegates.observable(Generator()) { property, oldValue, newValue ->
        newValue.generator?.let { easyGenerator = EasyGeneratorLoader(it) }
        newValue
    }
    var byteArrayImage: ByteArray? = null
    var fullImage: ByteArray? = null
    var generatorIndex: Int? = null
    var direction: FloatArray? = null
    val croppedPoint = SettingsHelper(context).getFaceLocation()
    val inliner = InlineImage()

    override fun disconnectFaceDetector() {
        interactor.faceDetection.release()
    }

    override fun readImageToBytes(imagePath: String?) {
        byteArrayImage = imagePath?.let { File(it).readBytes() }
    }

    fun shareImageToOtherApps() {
        if (interactor.checkIfPermissionGranted(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            val bitmap = imageFromGallery?.let { changePixelToBitmap(it) }
            val watermarkBitmap = interactor.placeWatermarkOnImage(bitmap)
            val shareIntent = interactor.getIntentForSharingImagesWithOtherApps(watermarkBitmap)
            view.shareImageToOtherApps(shareIntent)
        } else {
            view.requestPermissionFromUser(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), SHARE_IMAGE_PERMISSION_REQUEST)
        }
    }

    override fun randomizeModel(progress: Int) {
        changeGanImageFromSlider(progress.negative1To1())
    }

    override fun getFaceCroppedOutOfImageIfNoFaceGetFullImage(imageWithFaces: Bitmap?, context: Context): Bitmap? {
        var image: Bitmap? = null
        try {
            image = if (imageWithFaces == null) {
                imageWithFaces
            } else {
                getCroppedFaceImagFromImageWithFaces(imageWithFaces)
            }
        } catch (exception: IOException) {
            Log.e("ModelFragment", exception.message)
        }
        return image
    }

    private fun getCroppedFaceImagFromImageWithFaces(imageWithFaces: Bitmap): Bitmap? {
        val croppedFaces: MutableList<FaceLocation> = interactor.getFacesFromBitmap(imageWithFaces, imageWithFaces.width, imageWithFaces.height, context)
        val faceImage = if (isFacesDetected(croppedFaces)) {
            croppedFaces[0].croppedFace
        } else {
            imageWithFaces
        }
        return faceImage
    }

    fun isFacesDetected(listOfFaces: MutableList<FaceLocation>): Boolean {
        return !listOfFaces.isEmpty()
    }

    override fun saveImageDisplayedToPhone(context: Context): Boolean {
        var isSaved = false
        if (interactor.checkIfPermissionGranted(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            var bitmap = imageFromGallery?.let { changePixelToBitmap(it) }
            val croppedPoint = SettingsHelper(context).getFaceLocation()
            if (bitmap != null && fullImage != null) {
                val inlineImage = inlineImage(byteArrayImage?.toBitmap()!!, bitmap, fullImage)
                val waterMarkImage = interactor.placeWatermarkOnImage(inlineImage)
                isSaved = ImageSaver().saveImageToInternalStorage(waterMarkImage, context)

            } else {
                isSaved = ImageSaver().saveImageToInternalStorage(bitmap, context)
            }
        } else {
            view.requestPermissionFromUser(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), SAVE_IMAGE_PERMISSION_REQUEST)
        }
        return isSaved
    }

    fun inlineImage(oldCroppedImage: Bitmap, newCroppedImage: Bitmap, fullImage: ByteArray?): Bitmap? {
        val image: Bitmap? = if (fullImage != null) {
            inliner.setBeforeAfterCropSizingRatio(oldCroppedImage, newCroppedImage)
            fullImage.toBitmap()?.let { inliner.inlineCroppedImageToFullImage(newCroppedImage, it, croppedPoint) }
        } else {
            newCroppedImage<commit_msg>croppedPoint not used<commit_after>package hypr.a255bits.com.hypr.ModelFragmnt

import android.content.Context
import android.content.pm.PackageManager
import android.content.res.AssetManager
import android.graphics.Bitmap
import android.os.Bundle
import android.util.Log
import android.view.MenuItem
import hypr.a255bits.com.hypr.Generator.Generator
import hypr.a255bits.com.hypr.GeneratorLoader.EasyGeneratorLoader
import hypr.a255bits.com.hypr.GeneratorLoader.FaceLocation
import hypr.a255bits.com.hypr.R
import hypr.a255bits.com.hypr.Util.*
import kotlinx.coroutines.experimental.android.UI
import kotlinx.coroutines.experimental.launch
import org.jetbrains.anko.coroutines.experimental.bg
import org.jetbrains.anko.toast
import java.io.File
import java.io.IOException
import kotlin.properties.Delegates


class ModelFragmentPresenter(val view: ModelFragmentMVP.view, val interactor: ModelInteractor, val context: Context, pbFile: File?) : ModelFragmentMVP.presenter {

    init {
        val cont = context
        launch(UI) {
            val imageBitmap = bg {
                loadGenerator(pbFile, cont.assets)
                if (byteArrayImage == null) {
                    byteArrayImage = changePixelToBitmap(easyGenerator.sampleImageWithoutImage())?.toByteArray()
                }
                val bitmap = byteArrayImage?.toBitmap()
                val faces = getFaceCroppedOutOfImageIfNoFaceGetFullImage(bitmap, cont)
                val transformedImage = sampleImage(faces)
                return@bg inlineImage(bitmap!!, transformedImage, fullImage)
            }
            view.displayFocusedImage(imageBitmap.await())
        }
    }

    val analytics = Analytics(context)
    var imageFromGallery: IntArray? = null
    val SHARE_IMAGE_PERMISSION_REQUEST = 10
    val SAVE_IMAGE_PERMISSION_REQUEST: Int = 11
    lateinit var easyGenerator: EasyGeneratorLoader
    var generator: Generator by Delegates.observable(Generator()) { property, oldValue, newValue ->
        newValue.generator?.let { easyGenerator = EasyGeneratorLoader(it) }
        newValue
    }
    var byteArrayImage: ByteArray? = null
    var fullImage: ByteArray? = null
    var generatorIndex: Int? = null
    var direction: FloatArray? = null
    val croppedPoint = SettingsHelper(context).getFaceLocation()
    val inliner = InlineImage()

    override fun disconnectFaceDetector() {
        interactor.faceDetection.release()
    }

    override fun readImageToBytes(imagePath: String?) {
        byteArrayImage = imagePath?.let { File(it).readBytes() }
    }

    fun shareImageToOtherApps() {
        if (interactor.checkIfPermissionGranted(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            val bitmap = imageFromGallery?.let { changePixelToBitmap(it) }
            val watermarkBitmap = interactor.placeWatermarkOnImage(bitmap)
            val shareIntent = interactor.getIntentForSharingImagesWithOtherApps(watermarkBitmap)
            view.shareImageToOtherApps(shareIntent)
        } else {
            view.requestPermissionFromUser(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), SHARE_IMAGE_PERMISSION_REQUEST)
        }
    }

    override fun randomizeModel(progress: Int) {
        changeGanImageFromSlider(progress.negative1To1())
    }

    override fun getFaceCroppedOutOfImageIfNoFaceGetFullImage(imageWithFaces: Bitmap?, context: Context): Bitmap? {
        var image: Bitmap? = null
        try {
            image = if (imageWithFaces == null) {
                imageWithFaces
            } else {
                getCroppedFaceImagFromImageWithFaces(imageWithFaces)
            }
        } catch (exception: IOException) {
            Log.e("ModelFragment", exception.message)
        }
        return image
    }

    private fun getCroppedFaceImagFromImageWithFaces(imageWithFaces: Bitmap): Bitmap? {
        val croppedFaces: MutableList<FaceLocation> = interactor.getFacesFromBitmap(imageWithFaces, imageWithFaces.width, imageWithFaces.height, context)
        val faceImage = if (isFacesDetected(croppedFaces)) {
            croppedFaces[0].croppedFace
        } else {
            imageWithFaces
        }
        return faceImage
    }

    fun isFacesDetected(listOfFaces: MutableList<FaceLocation>): Boolean {
        return !listOfFaces.isEmpty()
    }

    override fun saveImageDisplayedToPhone(context: Context): Boolean {
        var isSaved = false
        if (interactor.checkIfPermissionGranted(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
            var bitmap = imageFromGallery?.let { changePixelToBitmap(it) }
            if (bitmap != null && fullImage != null) {
                val inlineImage = inlineImage(byteArrayImage?.toBitmap()!!, bitmap, fullImage)
                val waterMarkImage = interactor.placeWatermarkOnImage(inlineImage)
                isSaved = ImageSaver().saveImageToInternalStorage(waterMarkImage, context)

            } else {
                isSaved = ImageSaver().saveImageToInternalStorage(bitmap, context)
            }
        } else {
            view.requestPermissionFromUser(arrayOf(android.Manifest.permission.WRITE_EXTERNAL_STORAGE), SAVE_IMAGE_PERMISSION_REQUEST)
        }
        return isSaved
    }

    fun inlineImage(oldCroppedImage: Bitmap, newCroppedImage: Bitmap, fullImage: ByteArray?): Bitmap? {
        val image: Bitmap? = if (fullImage != null) {
            inliner.setBeforeAfterCropSizingRatio(oldCroppedImage, newCroppedImage)
            fullImage.toBitmap()?.let { inliner.inlineCroppedImageToFullImage(newCroppedImage, it, croppedPoint) }
        } else {
            newCroppedImage
{'fraction_non_alphanumeric': 0.08179489444395371, 'fraction_numerical': 0.004416570974295557, 'mean_word_length': 3.0932754880694144, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3023710', 'n_tokens_mistral': 3265, 'n_tokens_neox': 3142, 'n_words': 570}