/// <reference path="../angular.js" />  
/// <reference path="../angular.min.js" />   
/// <reference path="../angular-animate.js" />   
/// <reference path="../angular-animate.min.js" />   


var app;


(function () {
    app = angular.module("RESTClientModule", ['ngAnimate']);
})();


app.controller("AngularJs_ImageController", function ($scope, $timeout, $rootScope, $window, $http) {

    //Global Variables
    $scope.date = new Date();
    $scope.MyName = "Shanu";
    $scope.usrName = "";
    $scope.AllQuestions;


    //Game Hidden Table row display 
    $scope.showGameStart = true;
    $scope.showGame = false;
    $scope.showresult = false;
    $scope.showlifeline = false;

    //Game Detail Display Variables
    $scope.Question = "";
    $scope.Option1 = "";
    $scope.Option2 = "";
    $scope.Option3 = "";
    $scope.Option4 = "";
    $scope.Answers = "";
    $scope.Resuts = "";
    $scope.ImageAnswer = "won.png";


    //Game variable declared
    $scope.questionCount = 1;
    $scope.totalPoints = 0;
    $scope.rowCount = 0;


    //To Display Game Lifeline Images and Status
    $scope.imgcomputer = "computerE.png";
    $scope.imgcomputerStatus = 0;

    $scope.img50 = "50E.png";
    $scope.img50Status = 0;
    $scope.fiftyElimination = 0;

    $scope.imgShanu = "shanuE.png";
    $scope.imgShanuStatus = 0;

    $scope.imgChange = "changeE.png";
    $scope.imgChangeStatus = 0;
    $scope.showlifelinetext = "";

    //To Display Game Lifeline Images and Status
    $scope.answer1ClickStatus =0;
    $scope.answer2ClickStatus = 0;
    $scope.answer3ClickStatus = 0;
    $scope.answer4ClickStatus = 0;

    //Timer to display the countdown
    $scope.counter = 45;
    var countdownStop;

    $scope.startGame = function () {

    if($scope.usrName=='')
    {
        alert("Enter Your Name to start the game !");
        $scope.showGameStart = true;
        $scope.showGame = false;
        $scope.showresult = false;
    }
    else
    {      
        selectGameDetails();
        $scope.timerCountDownStart();
        alert($scope.usrName);
        $scope.showGameStart = false;
        $scope.showGame = true;
        $scope.showresult = false;
        }
    }


    //get all Game Details from Database
    function selectGameDetails() {
        $http.get('/api/Genius/').success(function (data) {
            $scope.AllQuestions = data;
            if ($scope.AllQuestions.length > 0) {
                $scope.Question = $scope.AllQuestions[$scope.rowCount].Question;
                $scope.Option1 = $scope.AllQuestions[$scope.rowCount].Option1;
                $scope.Option2 = $scope.AllQuestions[$scope.rowCount].Option2;
                $scope.Option3 = $scope.AllQuestions[$scope.rowCount].Option3;
                $scope.Option4 = $scope.AllQuestions[$scope.rowCount].Option4;

                $scope.Answers = $scope.AllQuestions[$scope.rowCount].Answer;

            }
        })
        .error(function () {
            $scope.error = "An Error has occured while loading posts!";

        });
    }

    $scope.timerCountDownStart = function () {
        countdownStop = $timeout(function () {
            console.log($scope.counter);
            if ($scope.counter >= 1) {
                $scope.counter--;
                $scope.timerCountDownStart();
            }
            else
            {
                $scope.timerCountDownStop();
                $scope.pointsCalculations(1);
                $scope.Resuts = "Sorry Time out " + $scope.usrName + " You lose the game :( .Your Total points are " + $scope.totalPoints;
                  alert($scope.Resuts)
                  $scope.ImageAnswer = "lose.png";
                  $scope.wrongAnswerandLoseGame();
            }
           
        }, 1000);
    };

    $scope.timerCountDownStop = function () {
        $timeout.cancel(countdownStop);
      
    } 


    //to check the Computer Lifeline already used or not If its not used then change the Computer image to Disabled Image.User can click (use only) one time
    $scope.funComputerLifeline = function () {
       
        if($scope.imgcomputerStatus==0)
        {

            $scope.imgcomputer = "computerD.png";
            $scope.imgcomputerStatus = 1;
            $scope.showlifelinetext = "Computer Says the Correct Answer might be   " + $scope.Answers + " Pick Your Choice ";
            alert($scope.showlifelinetext)
            $scope.showlifeline = true;
        }
    }


    //to check the 50/50 Lifeline already used or not If its not used then change the 50/50 image to Disabled Image.User can click (use only) one time
    $scope.funFiftyLifeline = function () {
        if ($scope.img50Status == 0) {

            if ($scope.Option1 != $scope.Answers) {
              
                if ($scope.fiftyElimination <= 1) {
                    $scope.Option1 = "";

                    $scope.fiftyElimination = $scope.fiftyElimination + 1;
                }
            }

            if ($scope.Option2 != $scope.Answers) {

                if ($scope.fiftyElimination <= 1) {
                    $scope.Option2 = "";

                    $scope.fiftyElimination = $scope.fiftyElimination + 1;
                }
            }

            if ($scope.Option3 != $scope.Answers) {

                if ($scope.fiftyElimination <= 1) {
                    $scope.Option3 = "";

                    $scope.fiftyElimination = $scope.fiftyElimination + 1;
                }
            }

            if ($scope.Option4 != $scope.Answers) {

                if ($scope.fiftyElimination <= 1) {
                    $scope.Option4 = "";

                    $scope.fiftyElimination = $scope.fiftyElimination + 1;
                }
            }

            $scope.img50 = "50D.png";
            $scope.img50Status = 1;
        }
    }

    //to check the Shanu Lifeline already used or not If its not used then change the Shanu image to Disabled Image.User can click (use only) one time
    $scope.funShanuLifeline = function () {
        if ($scope.imgShanuStatus == 0) {
            $scope.imgShanu = "shanuD.png";
            $scope.imgShanuStatus = 1;

            $scope.showlifelinetext = "Shanu Says the Correct Answer might be   " + $scope.Answers + " Pick Your Choice ";
            alert($scope.showlifelinetext)
            $scope.showlifeline = true;
        }
    }

    //to check the Change Question Lifeline already used or not If its not used then change the Change Question image to Disabled Image.User can click (use only) one time
    $scope.funChangeLifeline = function () {
        if ($scope.imgChangeStatus == 0) {
            $scope.imgChange = "changeD.png";
            $scope.imgChangeStatus = 1;
            $scope.NextQuestion(1);
        }
    }

    //Answer A Clicked
    $scope.answerAClicked = function () {
    
        if ($scope.Option1 == $scope.Answers)
        {
            $scope.answer1ClickStatus = 2;
            if ($scope.questionCount <= 14) {
                $scope.NextQuestion(0);
            }
            else if ($scope.questionCount == 15) {
                $scope.Resuts = "Wow " + $scope.usrName + " :) you won the game .You are the real Genius .Your total points is  1 Million :)";
                alert($scope.Resuts)
                $scope.ImageAnswer = "won.png";
                $scope.wrongAnswerandLoseGame();
            }
        }
        else
        {
            $scope.answer1ClickStatus = 3;
            $scope.wrongAnswerPoints();          

            $scope.Resuts = "Sorry Wrong Answer Dear  " + $scope.usrName + " You lose the game :( .Your Total points are " + $scope.totalPoints;
            alert($scope.Resuts)
            $scope.ImageAnswer = "lose.png";
            $scope.wrongAnswerandLoseGame();
           
        }
   
    }
    //for calculating wrong answer points

    $scope.wrongAnswerPoints = function () {
        if ($scope.questionCount <= 4)
        {
            $scope.totalPoints = "0";
        }
        else if ($scope.questionCount <= 9) {
            $scope.totalPoints = "1,000";
        }
        else if ($scope.questionCount <= 14) {
            $scope.totalPoints = "32,000";
        }
    }

    //Answer B Clicked
    $scope.answerBClicked = function () {

      if ($scope.Option2 == $scope.Answers) {
          $scope.answer2ClickStatus = 2;
          if ($scope.questionCount <= 14) {
              $scope.NextQuestion(0);
          }
          else if ($scope.questionCount == 15) {
              $scope.Resuts = "Wow " + $scope.usrName + " :) you won the game .You are the real Genius .Your total points is  1 Million :)";
              alert($scope.Resuts)
              $scope.ImageAnswer = "won.png";
              $scope.wrongAnswerandLoseGame();
          }
        }
        else {
          $scope.answer2ClickStatus = 3;
          $scope.wrongAnswerPoints();
            $scope.Resuts = "Sorry Wrong Answer Dear  " + $scope.usrName + " You lose the game :( .Your Total points are " + $scope.totalPoints;
            alert($scope.Resuts)
            $scope.ImageAnswer = "lose.png";
            $scope.wrongAnswerandLoseGame();
        }
    }

    //Answer C Clicked
    $scope.answerCClicked = function () {
     
        if ($scope.Option3 == $scope.Answers) {
            $scope.answer3ClickStatus = 2;
            if ($scope.questionCount <= 14) {
                $scope.NextQuestion(0);
            }
            else if ($scope.questionCount == 15) {
                $scope.Resuts = "Wow " + $scope.usrName + " :) you won the game .You are the real Genius .Your total points is  1 Million :)";
                alert($scope.Resuts)
                $scope.ImageAnswer = "won.png";
                $scope.wrongAnswerandLoseGame();
            }
        }
        else {
            $scope.answer3ClickStatus = 3;
            $scope.wrongAnswerPoints();
            $scope.Resuts = "Sorry Wrong Answer Dear  " + $scope.usrName + " You lose the game :( .Your Total points are " + $scope.totalPoints;
            alert($scope.Resuts)
            $scope.ImageAnswer = "lose.png";
            $scope.wrongAnswerandLoseGame();
        }
    }


    //Answer D Clicked
    $scope.answerDClicked = function () {
  
        if ($scope.Option4 == $scope.Answers) {
            $scope.answer4ClickStatus = 2;
            if ($scope.questionCount <= 14) {
                $scope.NextQuestion(0);
            }
            else if($scope.questionCount == 15)
            {
                $scope.Resuts = "Wow " + $scope.usrName + " :) you won the game .You are the real Genius .Your total points is  1 Million :)";
                alert($scope.Resuts)
                $scope.ImageAnswer = "won.png";
                $scope.wrongAnswerandLoseGame();
            }
        }
        else {
            $scope.answer4ClickStatus = 3;
            $scope.wrongAnswerPoints();
            $scope.Resuts = "Sorry Wrong Answer Dear  " + $scope.usrName + " You lose the game :( .Your Total points are " + $scope.totalPoints;
            alert($scope.Resuts)
            $scope.ImageAnswer = "lose.png";
            $scope.wrongAnswerandLoseGame();
        }
    }
    $scope.pointsCalculations = function (pointstype) {
        var questionCountChk=$scope.questionCount;
        //This is for the time out .For time out we will check the current question before value
        if (pointstype == 1)
        {
            questionCountChk = questionCountChk - 1;
        }

        switch (questionCountChk) {
            case 1:
                $scope.totalPoints="100";
                break;
            case 2:
                $scope.totalPoints="200";
                break;
            case 3:
                $scope.totalPoints ="300";
                break;
            case 4:
                $scope.totalPoints = "500";
                break;
            case 5:
                $scope.totalPoints = "1,000";
                break;
            case 6:
                $scope.totalPoints = "2,000";
                break;
            case 7:
                $scope.totalPoints = "4,000";
                break;
            case 8:
                $scope.totalPoints = "8,000";
                break;
            case 9:
                $scope.totalPoints = "16,000";
                break;
            case 10:
                $scope.totalPoints = "32,000";
                break;
            case 11:
                $scope.totalPoints = "64,000";
                break;
            case 12:
                $scope.totalPoints = "125,000";
                break;
            case 13:
                $scope.totalPoints = "250,000";
                break;
            case 14:
                $scope.totalPoints = "500,000";
                break;
            case 15:
                $scope.totalPoints = "1000,000";
                break;
            default:
                $scope.totalPoints ="0";
        } 
    }

    // If answer correctly then display the next Question
    $scope.NextQuestion = function (nextQuestionCount) {
        
        if (nextQuestionCount == 1) {
            $scope.rowCount = $scope.rowCount + 1;

            $scope.Question = $scope.AllQuestions[$scope.rowCount].Question;
            $scope.Option1 = $scope.AllQuestions[$scope.rowCount].Option1;
            $scope.Option2 = $scope.AllQuestions[$scope.rowCount].Option2;
            $scope.Option3 = $scope.AllQuestions[$scope.rowCount].Option3;
            $scope.Option4 = $scope.AllQuestions[$scope.rowCount].Option4;

            $scope.Answers = $scope.AllQuestions[$scope.rowCount].Answer;

            $scope.answer1ClickStatus = 0;
            $scope.answer2ClickStatus = 0;
            $scope.answer3ClickStatus = 0;
            $scope.answer4ClickStatus = 0;

            $scope.showlifeline = false;
            $scope.showlifelinetext = "";
        }
        else {
            if ($scope.questionCount <= 14) {
                var strconfirm = confirm("Do you Like to Continue or Walk Away.if you would like to continue the game click YES if you want to Quit game and go with the money then click Cancel?");
                if (strconfirm != true) {
                    $scope.pointsCalculations(0);
                    $scope.Resuts = "Thank You for Playing   " + $scope.usrName + " Your Total points are " + $scope.totalPoints;
                    alert($scope.Resuts)
                    $scope.ImageAnswer = "won.png";
                    $scope.wrongAnswerandLoseGame();
                    return true;
                }
            }

            $scope.questionCount = $scope.questionCount + 1;

            $scope.timerCountDownStart();
            $scope.counter = 45;
            $scope.timerCountDownStop();

            $scope.rowCount = $scope.rowCount + 1;

            $scope.Question = $scope.AllQuestions[$scope.rowCount].Question;
            $scope.Option1 = $scope.AllQuestions[$scope.rowCount].Option1;
            $scope.Option2 = $scope.AllQuestions[$scope.rowCount].Option2;
            $scope.Option3 = $scope.AllQuestions[$scope.rowCount].Option3;
            $scope.Option4 = $scope.AllQuestions[$scope.rowCount].Option4;

            $scope.Answers = $scope.AllQuestions[$scope.rowCount].Answer;

            $scope.answer1ClickStatus = 0;
            $scope.answer2ClickStatus = 0;
            $scope.answer3ClickStatus = 0;
            $scope.answer4ClickStatus = 0;

            $scope.showlifeline = false;
            $scope.showlifelinetext = "";
        }
    }

   
  $scope.wrongAnswerandLoseGame = function () {
        $scope.showGameStart = false;
        $scope.showGame = false;
        $scope.showresult = true;
        $scope.showlifeline = false;
  }

  $scope.NewQuestion = function () {
      $scope.usrName = "";
      $scope.counter = 45;
      $scope.questionCount = 1;
      $scope.totalPoints = 0;    
      $scope.rowCount = 0;

      $scope.answer1ClickStatus = 0;
      $scope.answer2ClickStatus = 0;
      $scope.answer3ClickStatus = 0;
      $scope.answer4ClickStatus = 0;

      $scope.imgcomputer = "computerE.png";
      $scope.imgcomputerStatus = 0;

      $scope.img50 = "50E.png";
      $scope.img50Status = 0;
      $scope.fiftyElimination = 0;

      $scope.imgShanu = "shanuE.png";
      $scope.imgShanuStatus = 0;

      $scope.imgChange = "changeE.png";

      $scope.imgChangeStatus = 0;
      $scope.timerCountDownStop();
      $scope.showGameStart = true;
      $scope.showGame = false;
      $scope.showresult = false;
      $scope.showlifeline = false;
      $scope.showlifelinetext = "";
  }

});