/*
 * Copyright (c) 2016 Applivery
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.applivery.applvsdklib.ui.views.feedback;

import android.graphics.Bitmap;
import com.applivery.applvsdklib.AppliverySdk;
import com.applivery.applvsdklib.domain.InteractorCallback;
import com.applivery.applvsdklib.domain.download.permissions.AccessNetworkStatePermission;
import com.applivery.applvsdklib.domain.feedback.FeedbackInteractor;
import com.applivery.applvsdklib.domain.model.ErrorObject;
import com.applivery.applvsdklib.domain.model.FeedBackType;
import com.applivery.applvsdklib.domain.model.Feedback;
import com.applivery.applvsdklib.domain.model.FeedbackResult;
import com.applivery.applvsdklib.domain.model.UserFeedback;
import com.applivery.applvsdklib.network.api.AppliveryApiService;
import com.applivery.applvsdklib.tools.androidimplementations.ScreenCaptureUtils;
import com.applivery.applvsdklib.tools.permissions.PermissionChecker;
import com.applivery.applvsdklib.tools.permissions.UserPermissionRequestResponseListener;
import com.applivery.applvsdklib.ui.model.ScreenCapture;
import com.applivery.applvsdklib.ui.views.ShowErrorAlert;

/**
 * Created by Sergio Martinez Rodriguez
 * Date 10/4/16.
 */
public class UserFeedbackPresenter implements InteractorCallback<FeedbackResult>{

  private final FeedbackView feedbackView;
  private final Feedback feedback;
  private AppliveryApiService appliveryApiService;
  private ScreenCapture screenCapture;
  final private PermissionChecker permissionRequestExecutor;
  final private AccessNetworkStatePermission accessNetworkStatePermission;

  public UserFeedbackPresenter(FeedbackView feedbackView) {
    this.feedbackView = feedbackView;
    this.feedback = new UserFeedback();
    this.permissionRequestExecutor = AppliverySdk.getPermissionRequestManager();
    this.accessNetworkStatePermission = new AccessNetworkStatePermission();
  }

  public void setAppliveryApiService(AppliveryApiService appliveryApiService) {
    this.appliveryApiService = appliveryApiService;
  }

  public void setScreenCapture(ScreenCapture screenCapture) {
    this.screenCapture = screenCapture;
  }

  public void initUi() {
    if (screenCapture == null) {
      feedbackView.checkScreenshotSwitch(false);
    } else {
      feedbackView.showFeedbackImage();
      feedbackView.showScheenShotPreview();
      feedbackView.checkScreenshotSwitch(true);
    }
  }

  public void cancelButtonPressed() {
    feedbackView.cleanScreenData();
    feedbackView.dismissFeedBack();
    screenCapture = null;
  }

  public void okButtonPressed() {
    okScreenShotPressed();
  }

  public void sendButtonPressed() {
    feedbackView.takeDataFromScreen();
  }

  public void feedbackButtonPressed() {
    feedback.setType(FeedBackType.FEEDBACK);
    feedbackView.setFeedbackButtonSelected();
  }

  public void bugButtonPressed() {
    feedback.setType(FeedBackType.BUG);
    feedbackView.setBugButtonSelected();
  }

  public void screenshotSwitchPressed(boolean activated) {
    feedback.attachScreenshot(activated);

    if (activated){
      feedbackView.showFeedbackImage();
    }else{
      feedbackView.hideFeedbackImage();
    }
  }

  public ScreenCapture getScreenCapture() {

    if (screenCapture == null){
      ScreenCapture screenCapture = ScreenCaptureUtils.getScreenCapture(AppliverySdk.getCurrentActivity());
      this.screenCapture = screenCapture;
    }

    return screenCapture;
  }

  public void updateScreenCaptureWith(Bitmap screenshot) {
    if (screenshot == null) {
      AppliverySdk.Logger.log("Cannot update ScreenCapture with a null bitmap.");
      return;
    }

    screenCapture = new ScreenCapture(screenshot);
    feedbackView.showFeedbackImage();
    feedbackView.checkScreenshotSwitch(true);
  }

  public void sendFeedbackInfo(String feedbackMessage, String screen) {
    if (permissionRequestExecutor == null) {
      AppliverySdk.Logger.log(
          "PermissionRequestExecutor must be initialized before accessing network state");
      return;
    }

    feedback.setMessage(feedbackMessage);
    feedback.setScreen(screen);

    if (feedback.mustAttachScreenshot()){
      feedback.setScreenCapture(screenCapture);
    }else{
      feedback.setScreenCapture(null);
    }

    if (!permissionRequestExecutor.isGranted(accessNetworkStatePermission)) {
      askForPermission();
    } else {
      sendFeedback();
    }

  }

  @Override public void onSuccess(FeedbackResult businessObject) {
    feedbackView.cleanScreenData();
    feedbackView.dismissFeedBack();
  }

  @Override public void onError(ErrorObject error) {
    ShowErrorAlert showErrorAlert = new ShowErrorAlert();
    showErrorAlert.showError(error);
  }

  public void feedbackImagePressed() {
    feedbackView.showScheenShotPreview();
  }

  public void okScreenShotPressed() {
    feedbackView.retrieveEditedScreenshot();
    feedbackView.hideScheenShotPreview();
  }

  private void askForPermission() {
    permissionRequestExecutor.askForPermission(accessNetworkStatePermission,
        new UserPermissionRequestResponseListener() {
          @Override public void onPermissionAllowed(boolean permissionAllowed) {
            if (permissionAllowed) {
              sendFeedback();
            }
          }
        }, AppliverySdk.getCurrentActivity());
  }

  private void sendFeedback() {
    AppliverySdk.getExecutor()
        .execute(FeedbackInteractor.getInstance(appliveryApiService, feedback, this));
  }
}

@@ -1806,16 +1806,17 @@
 kResult%3E
+ 
 %7B%0A%0A  pri
@@ -3586,16 +3586,17 @@
 tivated)
+ 
 %7B%0A      
@@ -3626,36 +3626,38 @@
 ckImage();%0A    %7D
+ 
 else
+ 
 %7B%0A      feedback
@@ -3768,16 +3768,17 @@
 == null)
+ 
 %7B%0A      
@@ -3806,16 +3806,26 @@
 apture =
+%0A         
  ScreenC
@@ -4676,16 +4676,17 @@
 nshot())
+ 
 %7B%0A      
@@ -4732,20 +4732,22 @@
 );%0A    %7D
+ 
 else
+ 
 %7B%0A      
@@ -4931,17 +4931,16 @@
 ;%0A    %7D%0A
-%0A
   %7D%0A%0A  @
@@ -4999,24 +4999,50 @@
 ssObject) %7B%0A
+    screenCapture = null;%0A
     feedback

Remove screen capture on send feedback