/*
 * 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.network.api.requests;

import com.applivery.applvsdklib.AppliverySdk;
import com.applivery.applvsdklib.domain.download.app.DownloadStatusListener;
import com.applivery.applvsdklib.domain.download.app.ExternalStorageWriter;
import com.applivery.applvsdklib.domain.model.BuildTokenInfo;
import com.applivery.applvsdklib.domain.model.BusinessObject;
import com.applivery.applvsdklib.domain.model.DownloadResult;
import com.applivery.applvsdklib.network.api.AppliveryApiService;
import java.io.InputStream;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Response;

/**
 * Created by Sergio Martinez Rodriguez
 * Date 9/1/16.
 */
public class DownloadBuildRequest extends ServerRequest {

  private final AppliveryApiService apiService;
  private final BuildTokenInfo token;
  private final DownloadStatusListener downloadStatusListener;
  private final ExternalStorageWriter externalStorageWriter;
  private final String appName;

  public DownloadBuildRequest(AppliveryApiService apiService, BuildTokenInfo token, String appName,
      DownloadStatusListener downloadStatusListener, ExternalStorageWriter externalStorageWriter) {

    this.externalStorageWriter = externalStorageWriter;

    this.downloadStatusListener = downloadStatusListener;
    this.apiService = apiService;
    this.token = token;
    this.appName = appName;
  }

  @Override protected BusinessObject performRequest() {
    DownloadResult downloadResult = new DownloadResult(false);

    Call<ResponseBody> response = apiService.downloadBuild(token.getBuild(), token.getToken());

    try {
      Response<ResponseBody> apiResponse = response.execute();

      int lenght = Integer.parseInt(apiResponse.headers().get("Content-Length"));
      InputStream in = apiResponse.body().byteStream();
      String apkFileName = appName + "_" + token.getBuild();

      String path =
          externalStorageWriter.writeToFile(in, lenght, downloadStatusListener, apkFileName);

      if (path != null) {
        downloadResult = new DownloadResult(true, path);
      }
    } catch (Exception e) {
      AppliverySdk.Logger.log(e.getMessage());
      downloadResult = new DownloadResult(e.getMessage());
    }

    return downloadResult;
  }
}

@@ -2264,24 +2264,66 @@
 execute();%0A%0A
+      if (apiResponse.body() != null) %7B%0A  
       int le
@@ -2388,24 +2388,26 @@
 -Length%22));%0A
+  
       InputS
@@ -2452,24 +2452,26 @@
 am();%0A      
+  
 String apkFi
@@ -2516,24 +2516,26 @@
 d();%0A%0A      
+  
 String path 
@@ -2536,16 +2536,18 @@
  path =%0A
+  
         
@@ -2635,24 +2635,26 @@
 me);%0A%0A      
+ 
+ 
 if (path != 
@@ -2653,32 +2653,34 @@
 path != null) %7B%0A
+  
         download
@@ -2720,16 +2720,192 @@
  path);%0A
+        %7D%0A      %7D else %7B%0A        AppliverySdk.Logger.log(%22Empty body response%22);%0A        downloadResult = new DownloadResult(%22Empty body response - %22 + apiResponse.message());%0A
       %7D%0A

Check if response body if null after download updated apk