/*
 * Copyright (c) 2017 Michael D'Arcy and Brianne O'Niel.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.group13.androidsdk.mycards;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class ReviewActivity extends AppCompatActivity {

    private ReviewManager reviewManager = null;
    private Card curCard = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_review);

        reviewManager = new ReviewManager(MyCardsDBManager.getInstance(this));
        loadNextReview();
    }

    private void onNoMoreCardsToReview() {
        setShowingCardFront();

        ((TextView) findViewById(R.id.tvFrontText)).setText("No more cards to review");
        findViewById(R.id.layoutReviewFrontButtons).setVisibility(View.GONE);
    }

    public void onShowAnswerClick(View v) {
        if (v.getId() != R.id.btnShowAnswer) {
            return;
        }
        if (reviewManager.getNumCards() <= 0) {
            onNoMoreCardsToReview();
            return;
        }
        setShowingCardBack();
    }

    private void setShowingCardBack() {
        findViewById(R.id.layoutReviewFrontButtons).setVisibility(View.GONE);
        findViewById(R.id.tvBackText).setVisibility(View.VISIBLE);
        findViewById(R.id.layoutScoreButtons).setVisibility(View.VISIBLE);
        if (curCard != null) {
            ((TextView) findViewById(R.id.tvBackText)).setText(curCard.getBackText());
        }
    }

    private void setShowingCardFront() {
        findViewById(R.id.layoutReviewFrontButtons).setVisibility(View.VISIBLE);
        findViewById(R.id.tvBackText).setVisibility(View.GONE);
        findViewById(R.id.layoutScoreButtons).setVisibility(View.GONE);
        if (curCard != null) {
            ((TextView) findViewById(R.id.tvFrontText)).setText(curCard.getFrontText());
        }
    }

    public void onScoreCardButtonClick(View v) {
        int score = -1;
        switch(v.getId()) {
            case R.id.btnRate0:
                score = 0;
                break;
            case R.id.btnRate1:
                score = 1;
                break;
            case R.id.btnRate2:
                score = 2;
                break;
            case R.id.btnRate3:
                score = 3;
                break;
            case R.id.btnRate4:
                score = 4;
                break;
            case R.id.btnRate5:
                score = 5;
                break;
            default:
                score = -1;
        }
        if(score == -1) {
            Log.e("score card btn click", "Unknown view clicked");
            return;
        }

        reviewManager.finishReview(curCard, score);
        loadNextReview();
    }

    private void loadNextReview() {
        if (reviewManager.getNumCards() <= 0) {
            curCard = null;
            onNoMoreCardsToReview();
        } else {
            curCard = reviewManager.getNextCard();
        }
        setShowingCardFront();
    }
}

@@ -787,38 +787,8 @@
 ew;%0A
-import android.widget.Button;%0A
 impo
@@ -2631,16 +2631,17 @@
   switch
+ 
 (v.getId
@@ -3207,16 +3207,17 @@
       if
+ 
 (score =
@@ -3624,30 +3624,24 @@
 Card();%0A    
-    %7D%0A
         setS
@@ -3655,20 +3655,30 @@
 ardFront();%0A
+        %7D%0A
     %7D%0A%7D%0A

Fix erroneously displaying button