package com.lukekorth.photo_paper.views;

import android.annotation.TargetApi;
import android.content.Context;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;

import com.google.firebase.analytics.FirebaseAnalytics;
import com.lukekorth.photo_paper.R;
import com.lukekorth.photo_paper.WallpaperApplication;
import com.lukekorth.photo_paper.helpers.Settings;
import com.lukekorth.photo_paper.models.ApiResponse;
import com.lukekorth.photo_paper.models.PhotoGalleryRequest;
import com.lukekorth.photo_paper.models.User;

import io.realm.Realm;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

import static com.lukekorth.photo_paper.helpers.Utils.dpToPx;

public class FavoriteButton extends Button implements View.OnClickListener {

    private int mUserId;
    private String mPhotoId;
    private boolean mFavorited;

    public FavoriteButton(Context context) {
        super(context);
        init();
    }

    public FavoriteButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public FavoriteButton(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public FavoriteButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        init();
    }

    private void init() {
        Realm realm = Realm.getDefaultInstance();
        mUserId = User.getUser(realm).getId();
        realm.close();
    }

    public void setup(String photoId, boolean favorited) {
        mPhotoId = photoId;
        mFavorited = favorited;

        setCompoundDrawablesWithIntrinsicBounds(R.drawable.button_action_favorite, 0, 0, 0);
        if (mFavorited) {
            setBackgroundResource(R.drawable.button_action_bg_favorited);
        } else {
            setBackgroundResource(R.drawable.button_action_bg);
        }

        setOnClickListener(this);
        setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (mFavorited) {
            setBackgroundResource(R.drawable.button_action_bg);

            WallpaperApplication.getApiClient()
                    .unfavorite(mUserId, Settings.getFavoriteGalleryId(getContext()), PhotoGalleryRequest.remove(mPhotoId))
                    .enqueue(new Callback<ApiResponse>() {
                        @Override
                        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                            if (response.isSuccess()) {
                                setup(mPhotoId, false);
                                FirebaseAnalytics.getInstance(getContext()).logEvent("unfavorited", null);
                            } else {
                                onFailure(null, null);
                            }
                        }

                        @Override
                        public void onFailure(Call<ApiResponse> call, Throwable t) {
                            setBackgroundResource(R.drawable.button_action_bg_favorited);
                        }
                    });
        } else {
            setBackgroundResource(R.drawable.button_action_bg_favorited);

            WallpaperApplication.getApiClient()
                    .favorite(mUserId, Settings.getFavoriteGalleryId(getContext()), PhotoGalleryRequest.add(mPhotoId))
                    .enqueue(new Callback<ApiResponse>() {
                        @Override
                        public void onResponse(Call<ApiResponse> call, Response<ApiResponse> response) {
                            if (response.isSuccess()) {
                                setup(mPhotoId, true);
                                FirebaseAnalytics.getInstance(getContext()).logEvent("favorited", null);
                            } else {
                                onFailure(null, null);
                            }
                        }

                        @Override
                        public void onFailure(Call<ApiResponse> call, Throwable t) {
                            setBackgroundResource(R.drawable.button_action_bg);
                        }
                    });
        }
    }

    @Override
    public void setBackgroundResource(int resid) {
        super.setBackgroundResource(resid);
        setPadding(dpToPx(getContext(), 4), dpToPx(getContext(), 4), dpToPx(getContext(), 4), dpToPx(getContext(), 4));
    }
}

@@ -977,32 +977,16 @@
 ntext);%0A
-        init();%0A
     %7D%0A%0A 
@@ -1080,32 +1080,16 @@
 attrs);%0A
-        init();%0A
     %7D%0A%0A 
@@ -1215,32 +1215,16 @@
 eAttr);%0A
-        init();%0A
     %7D%0A%0A 
@@ -1433,49 +1433,66 @@
     
-    init();%0A    %7D%0A%0A    private void init(
+%7D%0A%0A    public void setup(String photoId, boolean favorited
 ) %7B%0A
@@ -1541,24 +1541,25 @@
 ance();%0A
+%0A
         
 mUserId 
@@ -1554,136 +1554,97 @@
     
-mUserId = 
+if (
 User.
-get
+is
 User
-(realm).getId();%0A        realm.close();%0A    %7D%0A%0A    public void setup(String photoId, boolean favorited) %7B%0A
+LoggedIn(realm)) %7B%0A            mUserId = User.getUser(realm).getId();%0A    
     
@@ -1675,16 +1675,20 @@
         
+    
 mFavorit
@@ -1712,16 +1712,20 @@
         
+  
+  
 setCompo
@@ -1801,32 +1801,36 @@
  0, 0);%0A        
+    
 if (mFavorited) 
@@ -1835,32 +1835,36 @@
 ) %7B%0A            
+  
+  
 setBackgroundRes
@@ -1909,32 +1909,36 @@
 rited);%0A        
+    
 %7D else %7B%0A       
@@ -1934,32 +1934,36 @@
 e %7B%0A            
+  
+  
 setBackgroundRes
@@ -1998,32 +1998,36 @@
 on_bg);%0A        
+    
 %7D%0A%0A        setOn
@@ -2021,16 +2021,20 @@
         
+  
+  
 setOnCli
@@ -2047,24 +2047,28 @@
 ener(this);%0A
+    
         setV
@@ -2092,16 +2092,50 @@
 SIBLE);%0A
+        %7D%0A%0A        realm.close();%0A
     %7D%0A%0A 

Fix crash on full screen photo when not logged in