<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class CommentController extends Controller
{
    
	private function like($comment, $myUser, $time, $currentRating, $rate){
		if($rate == 0){ // unlike
			DB::table('comment_ratings')->where(['cid' => $comment->id, 'uid' => $myUser->id])->delete(); // delete the like
			DB::table('comments')->where('id', $comment->id)->decrement('likes');

			if(!is_null($myUser)){
				DB::table('users')->where('id', $myUser->id)->decrement('likes_given');
			}
			if($comment->created_by){
				DB::table('users')->where('id', $comment->created_by)->decrement('likes_received');
			}
		}else{ // like
			DB::table('comment_ratings')->insert(['cid' => $comment->id, 'uid' => $myUser->id, 'rating' => 1, 'created_at' => $time]); // add the like
			DB::table('comments')->where('id', $comment->id)->increment('likes');
			if(!is_null($myUser)){
				DB::table('users')->where('id', $myUser->id)->increment('likes_given');
			}
			if($comment->created_by){
				DB::table('users')->where('id', $comment->created_by)->increment('likes_received');
			}
		}
	}

	private function dislike($comment, $myUser, $time, $currentRating, $rate){
		if($rate == 0){ // undislike
			DB::table('comment_ratings')->where(['cid' => $comment->id, 'uid' => $myUser->id])->delete(); // delete the dislike
			DB::table('comments')->where('id', $comment->id)->decrement('dislikes');

			if(!is_null($myUser)){
				DB::table('users')->where('id', $myUser->id)->decrement('dislikes_given');
			}
			if($comment->created_by){
				DB::table('users')->where('id', $comment->created_by)->decrement('dislikes_received');
			}
		}else{ // dislike
			DB::table('comment_ratings')->insert(['cid' => $comment->id, 'uid' => $myUser->id, 'rating' => 0, 'created_at' => $time]); // add the dislike
			DB::table('comments')->where('id', $comment->id)->increment('dislikes');
			if(!is_null($myUser)){
				DB::table('users')->where('id', $myUser->id)->increment('dislikes_given');
			}
			if($comment->created_by){
				DB::table('users')->where('id', $comment->created_by)->increment('dislikes_received');
			}
		}
	}

	public function rate(){
		$myUser = newGetUser();
		if(is_null($myUser)){
			return $this->jsonError();
		}

		$pasteName = Input::get('pname');
		if(empty($pasteName)){
			return $this->jsonError();
        }
        
		$commentId = Input::get('cid');
		if(empty($commentId)){
			return $this->jsonError();
		}

		$paste = Paste::where('link', $pasteName)->first();
		if(is_null($paste) || $paste->hidden){ // if paste doesnt exist or is hidden
			return $this->jsonError();
        }
        
		$comment = DB::table('comments')->where('id', $commentId)->first();
		if(is_null($paste) || $comment->hidden){ // if comment doesnt exist or is hidden
			return $this->jsonError();
		}

        if($comment->doxId != $paste->id){ // validation to try to stop spamming with incrememnting comment id
			return $this->jsonError();
        }

		$rate = Input::get('rating');

		if($comment->created_by == $myUser->id){ // trying to rate own commet
			return $this->jsonError('cant like/dislike ur own comment');
		}

		if($rate != 1 && $rate != 0){ // invalid ratng
			return $this->jsonError();
		}

		$rating = DB::table('comment_ratings')->where(['cid' => $comment->id, 'uid' => $myUser->id])->first();
		
		$currentRating = (isset($rating->rating) ? $rating->rating : null);
		$time = Carbon\Carbon::now();
		$d = 0;
		$l = 0;
		DB::transaction(function() use ($rate, $comment, $myUser, $time, $rating, $currentRating, &$l, &$d) { // br3nt0n 2021
			if(!is_null($currentRating)){ // already rated, nigea
				if($rate == 1){
					if(!is_null($currentRating) && $currentRating == 1){ // just unlike
						$this->like($comment, $myUser, $time, $currentRating, 0);
						$l = 0;
						$d = 0;
					}else if(!is_null($currentRating) && $currentRating == 0){ // undislike & like
						$this->dislike($comment, $myUser, $time, $currentRating, 0);
						$this->like($comment, $myUser, $time, $currentRating, 1);
						$l = 1;
						$d = 0;
					}
				}else if($rate == 0){
					if(!is_null($currentRating) && $currentRating == 0){ // just undislike
						$this->dislike($comment, $myUser, $time, $currentRating, 0);
						$l = 0;
						$d = 0;
					}else if(!is_null($currentRating) && $currentRating == 1){ // unlike & dislike
						$this->like($comment, $myUser, $time, $currentRating, 0);
						$this->dislike($comment, $myUser, $time, $currentRating, 1);
						$l = 0;
						$d = 1;
					}
				}
			}else{
				if($rate == 1){ // just like
					$this->like($comment, $myUser, $time, $currentRating, 1);
					$l = 1;
					$d = 0;
				}else if($rate == 0){ // just dislike
					$this->dislike($comment, $myUser, $time, $currentRating, 1);
					$l = 0;
					$d = 1;
				}
			}
		});
		return response()->json(['status' => 'done', 'l' => $l, 'd' => $d]);
	}
}
