package org.lagonette.android.api.response;

import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.util.Log;

import com.google.firebase.crash.FirebaseCrash;
import com.google.gson.annotations.SerializedName;

import org.lagonette.android.room.entity.PartnerMetadata;
import org.lagonette.android.room.entity.special.OfficePartner;
import org.lagonette.android.room.entity.special.OfficePartnerMetadata;
import org.lagonette.android.util.PreferenceUtil;

import java.util.List;

public class PartnersResponse extends ApiResponse {

    private static final String TAG = "PartnersResponse";

    @SerializedName("partners")
    private List<Partner> mPartners;

    public boolean prepareInsert(
            @NonNull Context context,
            @NonNull List<org.lagonette.android.room.entity.Partner> partners,
            @NonNull List<org.lagonette.android.room.entity.PartnerMetadata> partnerMetadataList,
            @NonNull List<org.lagonette.android.room.entity.PartnerSideCategory> partnerSideCategories) {

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);

        String md5Sum = preferences.getString(
                PreferenceUtil.KEY_PARTNER_MD5_SUM,
                PreferenceUtil.DEFAULT_VALUE_PARTNER_MD5_SUM
        );


        if (!mMd5Sum.equals(md5Sum)) {

            for (Partner partner : mPartners) {
                partner.prepareInsert(partners, partnerMetadataList, partnerSideCategories);
            }
            addOfficePartner(partners, partnerMetadataList);

            doSomeCheck(partners);

            // TODO Ensure data are saved before saving md5 sum, maybe put this in a runnable an execute it after closing transaction
            preferences.edit()
                    .putString(PreferenceUtil.KEY_PARTNER_MD5_SUM, mMd5Sum)
                    .apply();
            return true;
        }

        return false;
    }

    // TODO Make a custom retrofit TypeConverter
    private void doSomeCheck(@NonNull List<org.lagonette.android.room.entity.Partner> partners) {
        boolean send = false;
        for (org.lagonette.android.room.entity.Partner partner : partners) {
            if (partner.latitude == 0 || partner.longitude == 0) {
                send = true;
                FirebaseCrash.logcat(Log.WARN, TAG, "Wrong coordinates " + partner.name + " (" + partner.id + "): (" + partner.latitude + ", " + partner.longitude + ")" );
            }

            if (TextUtils.isEmpty(partner.description)) {
                send = true;
                FirebaseCrash.logcat(Log.WARN, TAG, "Empty description " + partner.name + " (" + partner.id + ")" );
            }

            if (TextUtils.isEmpty(partner.shortDescription)) {
                send = true;
                FirebaseCrash.logcat(Log.WARN, TAG, "Empty short description " + partner.name + " (" + partner.id + ")" );
            }

            if (TextUtils.isEmpty(partner.address.street)
                    || TextUtils.isEmpty(partner.address.city)
                    || TextUtils.isEmpty(partner.address.zipCode)) {
                send = true;
                FirebaseCrash.logcat(Log.WARN, TAG, "Empty address " + partner.name + " (" + partner.id + ")" );
            }
        }

        if (send) {
            // TODO Use crashlitics
            FirebaseCrash.report(new Exception("API send incomplete data."));
        }

    }

    private void addOfficePartner(@NonNull List<org.lagonette.android.room.entity.Partner> partners, @NonNull List<PartnerMetadata> partnerMetadataList) {
//        partners.add(new OfficePartner());
//        partnerMetadataList.add(new OfficePartnerMetadata());
    }
}
@@ -2487,53 +2487,246 @@
 ates
- %22 + partner.name + %22 (%22 + partner.id + %22)
+: %5B id: %22 + partner.id + %22, name: %22 + partner.name + %22%5D%22);%0A            %7D%0A%0A            if (TextUtils.isEmpty(partner.description)) %7B%0A                send = true;%0A                FirebaseCrash.logcat(Log.WARN, TAG, %22Empty description: %5Bid
 : 
-(
 %22 + 
@@ -2737,21 +2737,21 @@
 ner.
-latitude + %22,
+id + %22, name:
  %22 +
@@ -2763,22 +2763,17 @@
 ner.
-longitud
+nam
 e + %22
-)
+%5D
 %22 );
@@ -2791,43 +2791,150 @@
   %7D%0A
-%0A            if (TextUtils.isEmpty(
+            else %7B%0A                String trimmedDescription = partner.description.trim();%0A                if (trimmedDescription.length() != 
 part
@@ -2940,37 +2940,49 @@
 tner.description
+.length(
 )) %7B%0A
+    
                 
@@ -2986,32 +2986,98 @@
    send = true;%0A
+                    partner.description = trimmedDescription;%0A    
                 
@@ -3105,39 +3105,33 @@
 Log.WARN, TAG, %22
-Empty d
+D
 escription %22 + p
@@ -3116,32 +3116,56 @@
 AG, %22Description
+ with extra spaces: %5Bid:
  %22 + partner.nam
@@ -3157,35 +3157,39 @@
 %22 + partner.
+id + %22, 
 name
- +
+:
  %22
- (%22
  + partner.i
@@ -3183,35 +3183,54 @@
 %22 + partner.
-id + %22)%22 );
+name + %22%5D%22);%0A                %7D
 %0A           
@@ -3401,16 +3401,22 @@
 cription
+: %5Bid:
  %22 + par
@@ -3416,34 +3416,38 @@
 %22 + partner.
-name + %22 (
+id + %22, name: 
 %22 + partner.
@@ -3438,39 +3438,41 @@
 me: %22 + partner.
-id
+name
  + %22
-)
+%5D
 %22 );%0A           
@@ -3760,16 +3760,22 @@
  address
+: %5Bid:
  %22 + par
@@ -3783,18 +3783,22 @@
 ner.
-name + %22 (
+id + %22, name: 
 %22 + 
@@ -3809,15 +3809,17 @@
 ner.
-id
+name
  + %22
-)
+%5D
 %22 );

Add 'extra spaces' description check