package com.ar.tothestars.ui;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.ar.tothestars.R;
import com.ar.tothestars.models.Photo;
import com.squareup.picasso.Picasso;

import uk.co.senab.photoview.PhotoView;

/**
 * Created by ariviere on 07/06/15.
 */
public class APODPhotoView extends RelativeLayout {

    private Photo mPhoto;

    private PhotoView mPhotoView;
    private TextView mErrorMessageView;

    public APODPhotoView(Context context) {
        super(context);

        if (!isInEditMode()) {
            init(context);
        }
    }

    public APODPhotoView(Context context, AttributeSet attrs) {
        super(context, attrs);

        if (!isInEditMode()) {
            init(context);
        }
    }

    public APODPhotoView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        if (!isInEditMode()) {
            init(context);
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
    }

    public void setModel(Photo photo) {
        mPhoto = photo;

        Picasso.with(getContext())
                .load(mPhoto.getUrl())
                .fit()
                .centerInside()
//                .transform(PaletteTransformation.instance())
                .into(mPhotoView, new com.squareup.picasso.Callback.EmptyCallback() {
                    @Override
                    public void onSuccess() {
//                        //get palette
//                        Bitmap bitmap = ((BitmapDrawable) mPhotoView.getDrawable()).getBitmap(); // Ew!
//                        Palette palette = PaletteTransformation.getPalette(bitmap);
//                        int mutedDark = palette.getDarkMutedColor(0x000000);
//                        mPhotoView.setBackgroundColor(mutedDark);

                        Log.d("APODPhotoView", "Successfully loaded " + mPhoto.getUrl());
                    }

                    @Override
                    public void onError() {
                        Log.d("APODPhotoView", "Failed to load " + mPhoto.getUrl());
                        mErrorMessageView.setVisibility(View.VISIBLE);
                    }
                });
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.item_photo, this);

        mPhotoView = (PhotoView) findViewById(R.id.photo);
        mErrorMessageView = (TextView) findViewById(R.id.error_message);
    }
}

@@ -491,16 +491,135 @@
 yout %7B%0A%0A
+    public static final String VIMEO = %22player.vimeo.com%22;%0A    public static final String YOUTUBE = %22www.youtube.com%22;%0A
     priv
@@ -1394,24 +1394,135 @@
 o = photo;%0A%0A
+        if (!isPhotoValid()) %7B%0A            mErrorMessageView.setVisibility(View.VISIBLE);%0A        %7D else %7B%0A    
         Pica
@@ -1540,24 +1540,28 @@
 tContext())%0A
+    
             
@@ -1607,15 +1607,23 @@
     
+    
 .fit()%0A
+    
     
@@ -1729,16 +1729,20 @@
         
+    
 .into(mP
@@ -1815,32 +1815,36 @@
                 
+ 
+   
 @Override%0A      
@@ -1829,32 +1829,36 @@
       @Override%0A
+    
                 
@@ -2283,32 +2283,36 @@
                 
+    
 Log.d(%22APODPhoto
@@ -2377,24 +2377,28 @@
             
+    
 %7D%0A%0A         
@@ -2400,32 +2400,36 @@
                 
+ 
+   
 @Override%0A      
@@ -2414,32 +2414,36 @@
       @Override%0A
+    
                 
@@ -2486,32 +2486,36 @@
                 
+  
+  
 Log.d(%22APODPhoto
@@ -2551,32 +2551,36 @@
 hoto.getUrl());%0A
+    
                 
@@ -2650,26 +2650,32 @@
             
-%7D%0A
+    %7D%0A  
             
@@ -2682,12 +2682,24 @@
     
+  
 %7D);%0A
+        %7D%0A
     
@@ -2953,11 +2953,242 @@
 );%0A    %7D
+%0A%0A    private boolean isPhotoValid() %7B%0A        if (mPhoto.getUrl().contains(VIMEO)) %7B%0A            return false;%0A        %7D else if (mPhoto.getUrl().contains(YOUTUBE)) %7B%0A            return false;%0A        %7D%0A%0A        return true;%0A    %7D
 %0A%7D%0A

handle error if youtube or vimeo