package net.squanchy.search;

import android.content.Context;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;

import net.squanchy.R;
import net.squanchy.imageloader.ImageLoader;
import net.squanchy.search.view.SpeakersView.OnSpeakerClickedListener;
import net.squanchy.speaker.domain.view.Speaker;
import net.squanchy.support.lang.Optional;

public class SearchItemView extends LinearLayout {

    private ImageView image;
    private TextView name;

    public SearchItemView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SearchItemView(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public SearchItemView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        super.setOrientation(VERTICAL);
    }

    @Override
    public void setOrientation(int orientation) {
        throw new UnsupportedOperationException("SearchItem doesn't support changing orientation");
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        image = (ImageView) findViewById(R.id.speaker_image);
        name = (TextView) findViewById(R.id.speaker_names);
    }

    public void updateWith(Speaker speaker, ImageLoader imageLoader, OnSpeakerClickedListener listener) {
        name.setText(speaker.name());
        updateSpeakerPhotos(speaker, imageLoader);
        setOnClickListener(v -> listener.onSpeakerClicked(speaker));
    }

    private void updateSpeakerPhotos(Speaker speaker, ImageLoader imageLoader) {
        if (imageLoader == null) {
            throw new IllegalStateException("Unable to access the ImageLoader, it hasn't been initialized yet");
        }

        Optional<String> avatarImageURL = speaker.avatarImageURL();
        if (avatarImageURL.isPresent()) {
            loadPhoto(image, avatarImageURL.get(), imageLoader);
        }
    }

    private void loadPhoto(ImageView photoView, Optional<String> photoUrl, ImageLoader imageLoader) {
        //TODO load photoUrl here instead of the hardcoded resource
        StorageReference photoReference = FirebaseStorage.getInstance().getReference("speakers/squanchy.webp");
        imageLoader.load(photoReference).into(photoView);
    }
}

@@ -2223,32 +2223,22 @@
 toView, 
-Optional%3C
 String
-%3E
  photoUr
@@ -2279,68 +2279,50 @@
     
-//TODO load photoUrl here instead of the hardcoded resource%0A
+if (isFirebaseStorageUrl(photoUrl)) %7B%0A    
     
@@ -2406,35 +2406,102 @@
 nce(
-%22speakers/squanchy.webp%22);%0A
+photoUrl);%0A            imageLoader.load(photoReference).into(photoView);%0A        %7D else %7B%0A    
     
@@ -2522,33 +2522,27 @@
 r.load(photo
-Reference
+Url
 ).into(photo
@@ -2544,20 +2544,166 @@
 photoView);%0A
+        %7D%0A    %7D%0A%0A    private boolean isFirebaseStorageUrl(String url) %7B%0A        return url.startsWith(%22gs://%22);            // TODO move elsewhere%0A
     %7D%0A%7D%0A

Load real photos for speakers in search