/**
  * Update the specified resource in storage.
  *
  * @param  int  $id
  * @return Response
  */
 public function update(InterestRequest $request, $username)
 {
     $interests = $request->except('_method');
     $user = MecanexUser::where('username', $username)->get()->first();
     if (empty($user)) {
         $response = ["error" => "User doesn`t exist"];
         $statusCode = 404;
     } elseif (count($user->interest) == 0) {
         $response = ["message" => "User has not set any interests, To create interests, try store"];
         $statusCode = 404;
     } else {
         //check if what sends is correct or else it crushes
         //updates interests and user scores
         $key_values = array();
         foreach ($interests as $key => $value) {
             $interest = Interest::where('short_name', $key)->get(array('id'))->first();
             $key_values[$interest->id] = $value;
             $user->interest()->sync([$interest->id => ['interest_score' => $value]], false);
             $term = Term::where('term', $key)->firstOrFail();
             $value = $value / 5;
             $user->term()->sync([$term->id => ['user_score' => $value]], false);
         }
         //there must be a more clever way to do this (foreach maybe)
         //created 2 arrays one with the term ids ($term_ids) and the other with the scores ($term_scores)
         $terms = Term::get(array('id'));
         $all_terms = [];
         foreach ($terms as $term) {
             array_push($all_terms, $term->id);
         }
         $all_terms_length = count($all_terms);
         ksort($key_values);
         $term_ids = array_keys($key_values);
         $term_ids_length = count($term_ids);
         //update adjacency matrix
         for ($i = 0; $i <= $term_ids_length - 1; $i++) {
             for ($j = 0; $j <= $all_terms_length - 1; $j++) {
                 if ($term_ids[$i] == $all_terms[$j]) {
                     continue;
                 } elseif ($term_ids[$i] > $all_terms[$j]) {
                     $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $all_terms[$j])->where('term_neighbor_id', $term_ids[$i])->get()->first();
                     $temp_user = $user->term->find($all_terms[$j]);
                     $user_term_score_a = $temp_user->pivot->user_score;
                     $temp_user = $user->term->find($term_ids[$i]);
                     $user_term_score_b = $temp_user->pivot->user_score;
                     $new_score = $user_term_score_b * $user_term_score_a;
                     //						}
                 } else {
                     $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $term_ids[$i])->where('term_neighbor_id', $all_terms[$j])->get()->first();
                     $temp_user = $user->term->find($all_terms[$j]);
                     $user_term_score_a = $temp_user->pivot->user_score;
                     $temp_user = $user->term->find($term_ids[$i]);
                     $user_term_score_b = $temp_user->pivot->user_score;
                     $new_score = $user_term_score_b * $user_term_score_a;
                 }
                 $temp_user_matrix->link_score = $new_score;
                 $temp_user_matrix->save();
             }
         }
         //update profile
         for ($j = 1; $j <= $all_terms_length; $j++) {
             $profile_score = 0;
             for ($i = 1; $i <= $all_terms_length; $i++) {
                 $temp_user = $user->term->find($i);
                 $user_term_score = $temp_user->pivot->user_score;
                 //get score of user
                 if ($i == $j) {
                     $link_score = 0;
                 } elseif ($i > $j) {
                     $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $j)->where('term_neighbor_id', $i)->get()->first();
                     $link_score = $temp_user_matrix->link_score;
                 } else {
                     $temp_user_matrix = MecanexUserTermHomeTermNeighbour::where('mecanex_user_id', $user->id)->where('term_home_id', $i)->where('term_neighbor_id', $j)->get()->first();
                     $link_score = $temp_user_matrix->link_score;
                 }
                 $profile_score = $profile_score + $user_term_score * $link_score;
             }
             $user->profilescore()->sync([$j => ['profile_score' => $profile_score]], false);
         }
         $response = ["message" => "User Interests were saved"];
         $statusCode = 201;
     }
     return response($response, $statusCode)->header('Content-Type', 'application/json');
 }
 private function createInterest(InterestRequest $request)
 {
     //need validation...what if a person doesn't attach any photos? What if a person attaches a different file than a .jpg?
     $interest = Auth::user()->interests()->create($request->all());
     $this->syncTags($interest, $request->input('tag_list'));
     //checks if a directory exists for the logged in user, if not, creates one for them
     File::exists(public_path() . '/images/' . Auth::user()->email . '/') or File::makeDirectory(public_path() . '/images/' . Auth::user()->email . '/');
     //checks if the default image exists in a user's folder...if not, copies it into that directory
     File::exists(public_path() . 'images/' . Auth::user()->email . '/lord.png') or File::copy(public_path() . '/images/lord.png', public_path() . '/images/' . Auth::user()->email . '/lord.png');
     //point to the default images, in case that user doesn't upload all the images.
     $default_image = '/images/' . Auth::user()->email . '/lord.png';
     $interest->image_1 = $default_image;
     $interest->image_2 = $default_image;
     $interest->image_3 = $default_image;
     $interest->image_1_thumb = $default_image;
     $interest->image_2_thumb = $default_image;
     $interest->image_3_thumb = $default_image;
     //Detect if the user is submitting an image...if so, change the name of that image to its path...also saves it locally. Also creates a thumbnail for the home and edit pages.
     if (Input::hasFile('image_1')) {
         $image_1 = Input::file('image_1');
         $filename = $interest->id . 'FirstImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_1->getRealPath())->save($path);
         $interest->image_1 = '/images/' . Auth::user()->email . '/' . $filename;
         $image_1_thumb = Input::file('image_1');
         $filename = $interest->id . 'Thumb-FirstImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_1_thumb->getRealPath())->resize(400, null, function ($constraint) {
             $constraint->aspectRatio();
         })->save($path);
         $interest->image_1_thumb = '/images/' . Auth::user()->email . '/' . $filename;
     }
     if (Input::hasFile('image_2')) {
         $image_2 = Input::file('image_2');
         $filename = $interest->id . 'SecondImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_2->getRealPath())->save($path);
         $interest->image_2 = '/images/' . Auth::user()->email . '/' . $filename;
         $image_2_thumb = Input::file('image_2');
         $filename = $interest->id . 'Thumb-SecondImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_2_thumb->getRealPath())->resize(400, null, function ($constraint) {
             $constraint->aspectRatio();
         })->save($path);
         $interest->image_2_thumb = '/images/' . Auth::user()->email . '/' . $filename;
     }
     if (Input::hasFile('image_3')) {
         $image_3 = Input::file('image_3');
         $filename = $interest->id . 'ThirdImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_3->getRealPath())->save($path);
         $interest->image_3 = '/images/' . Auth::user()->email . '/' . $filename;
         $image_3_thumb = Input::file('image_3');
         $filename = $interest->id . 'Thumb-ThirdImage.jpg';
         $path = public_path('/images/' . Auth::user()->email . '/' . $filename);
         Image::make($image_3_thumb->getRealPath())->resize(400, null, function ($constraint) {
             $constraint->aspectRatio();
         })->save($path);
         $interest->image_3_thumb = '/images/' . Auth::user()->email . '/' . $filename;
     }
     //Detect if user is submitting video links...if so, get rid of the default photo, then concert the link to allow for a Pornhub
     //embed. We need to add functionality here to allow for other p**n sites as well. Currently, the display only allows for
     //pornhub links but this must change.
     if (strpos($interest->video_1, "pornhub") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://www.pornhub.com/embed/" . $video_1_id;
     } elseif (strpos($interest->video_1, "cliphunter") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://www.cliphunter.com/embed/" . $video_1_id;
     } elseif (strpos($interest->video_1, "youporn") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://www.youporn.com/embed/" . $video_1_id;
     } elseif (strpos($interest->video_1, "youjizz") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://www.youjizz.com/videos/embed/" . $video_1_id;
     } elseif (strpos($interest->video_1, "xvideos") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://flashservice.xvideos.com/embedframe/" . $video_1_id;
     } elseif (strpos($interest->video_1, "xhamster") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://xhamster.com/xembed.php?video=" . $video_1_id;
     } elseif (strpos($interest->video_1, "tube8") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://xhamster.com/xembed.php?video=" . $video_1_id;
     } elseif (strpos($interest->video_1, "spankwire") !== false) {
         if (preg_match_all('/\\d+/', $interest->video_1, $numbers)) {
             $video_1_id = end($numbers[0]);
         }
         $interest->video_1 = "http://www.spankwire.com/EmbedPlayer.aspx?ArticleId=" . $video_1_id;
     } elseif (strpos($interest->video_1, "redtube") !== false) {
         $video_1_id = preg_replace('/\\D/', '', $interest->video_1);
         $interest->video_1 = "http://embed.redtube.com/?id=" . $video_1_id . "&bgcolor=000000";
     } else {
         if (in_array("1", $request->input('tag_list'))) {
             $videos = Tag::find(1)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("2", $request->input('tag_list'))) {
             $videos = Tag::find(2)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("3", $request->input('tag_list'))) {
             $videos = Tag::find(3)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("4", $request->input('tag_list'))) {
             $videos = Tag::find(4)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("5", $request->input('tag_list'))) {
             $videos = Tag::find(5)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("6", $request->input('tag_list'))) {
             $videos = Tag::find(6)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("7", $request->input('tag_list'))) {
             $videos = Tag::find(7)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         } elseif (in_array("8", $request->input('tag_list'))) {
             $videos = Tag::find(8)->videos;
             $thevideo = $videos->random();
             $interest->video_1 = $thevideo->htmlembedcode;
         }
     }
     //Video 2
     if (strpos($interest->video_2, "pornhub") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://www.pornhub.com/embed/" . $video_2_id;
     } elseif (strpos($interest->video_2, "cliphunter") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://www.cliphunter.com/embed/" . $video_2_id;
     } elseif (strpos($interest->video_2, "youporn") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://www.youporn.com/embed/" . $video_2_id;
     } elseif (strpos($interest->video_2, "youjizz") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://www.youjizz.com/videos/embed/" . $video_2_id;
     } elseif (strpos($interest->video_2, "xvideos") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://flashservice.xvideos.com/embedframe/" . $video_2_id;
     } elseif (strpos($interest->video_2, "xhamster") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://xhamster.com/xembed.php?video=" . $video_2_id;
     } elseif (strpos($interest->video_2, "tube8") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://xhamster.com/xembed.php?video=" . $video_2_id;
     } elseif (strpos($interest->video_2, "spankwire") !== false) {
         if (preg_match_all('/\\d+/', $interest->video_2, $numbers)) {
             $video_2_id = end($numbers[0]);
         }
         $interest->video_2 = "http://www.spankwire.com/EmbedPlayer.aspx?ArticleId=" . $video_2_id;
     } elseif (strpos($interest->video_2, "redtube") !== false) {
         $video_2_id = preg_replace('/\\D/', '', $interest->video_2);
         $interest->video_2 = "http://embed.redtube.com/?id=" . $video_2_id . "&bgcolor=000000";
     } else {
         if (in_array("1", $request->input('tag_list'))) {
             $videos = Tag::find(1)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("2", $request->input('tag_list'))) {
             $videos = Tag::find(2)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("3", $request->input('tag_list'))) {
             $videos = Tag::find(3)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("4", $request->input('tag_list'))) {
             $videos = Tag::find(4)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("5", $request->input('tag_list'))) {
             $videos = Tag::find(5)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("6", $request->input('tag_list'))) {
             $videos = Tag::find(6)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("7", $request->input('tag_list'))) {
             $videos = Tag::find(7)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         } elseif (in_array("8", $request->input('tag_list'))) {
             $videos = Tag::find(8)->videos;
             $thevideo = $videos->random();
             $interest->video_2 = $thevideo->htmlembedcode;
         }
     }
     //video 3
     if (strpos($interest->video_3, "pornhub") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://www.pornhub.com/embed/" . $video_3_id;
     } elseif (strpos($interest->video_3, "cliphunter") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://www.cliphunter.com/embed/" . $video_3_id;
     } elseif (strpos($interest->video_3, "youporn") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://www.youporn.com/embed/" . $video_3_id;
     } elseif (strpos($interest->video_3, "youjizz") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://www.youjizz.com/videos/embed/" . $video_3_id;
     } elseif (strpos($interest->video_3, "xvideos") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://flashservice.xvideos.com/embedframe/" . $video_3_id;
     } elseif (strpos($interest->video_3, "xhamster") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://xhamster.com/xembed.php?video=" . $video_3_id;
     } elseif (strpos($interest->video_3, "tube8") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://xhamster.com/xembed.php?video=" . $video_3_id;
     } elseif (strpos($interest->video_3, "spankwire") !== false) {
         if (preg_match_all('/\\d+/', $interest->video_3, $numbers)) {
             $video_3_id = end($numbers[0]);
         }
         $interest->video_3 = "http://www.spankwire.com/EmbedPlayer.aspx?ArticleId=" . $video_3_id;
     } elseif (strpos($interest->video_3, "redtube") !== false) {
         $video_3_id = preg_replace('/\\D/', '', $interest->video_3);
         $interest->video_3 = "http://embed.redtube.com/?id=" . $video_3_id . "&bgcolor=000000";
     } else {
         if (in_array("1", $request->input('tag_list'))) {
             $videos = Tag::find(1)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("2", $request->input('tag_list'))) {
             $videos = Tag::find(2)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("3", $request->input('tag_list'))) {
             $videos = Tag::find(3)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("4", $request->input('tag_list'))) {
             $videos = Tag::find(4)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("5", $request->input('tag_list'))) {
             $videos = Tag::find(5)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("6", $request->input('tag_list'))) {
             $videos = Tag::find(6)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("7", $request->input('tag_list'))) {
             $videos = Tag::find(7)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         } elseif (in_array("8", $request->input('tag_list'))) {
             $videos = Tag::find(8)->videos;
             $thevideo = $videos->random();
             $interest->video_3 = $thevideo->htmlembedcode;
         }
     }
     //save the interest after all of these changes
     $interest->save();
     return $interest;
 }