package com.google.sps.data;

import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;

public final class Card {

    private String blobKey;
    private String labels;
    private String fromLang;
    private String toLang;
    private String textNotTranslated;
    private String textTranslated;
    private String cardKey;
      
    public Card (
        String blobKey,
        String labels,
        String fromLang,
        String toLang,
        String textNotTranslated,
        String textTranslated) {
            
      this.blobKey = blobKey;
      this.labels = labels;
      this.fromLang = fromLang;
      this.toLang = toLang;
      this.textNotTranslated = textNotTranslated;
      this.textTranslated = textTranslated;
      this.cardKey = "null";
    }

    public String getBlobKey() {
        return this.blobKey;
    }

    public String getLabels() {
        return this.labels;
    }

    public String getFromLang() {
        return this.fromLang;
    }

    public String getToLang() {
        return this.toLang;
    }

    public String getTextNotTranslated() {
        return this.textNotTranslated;
    }

    public String getTextTranslated() {
        return this.textTranslated;
    }

    public String getCardKey() {
        return this.cardKey;
    }

    public void setBlobKey(String newBlobKey) {
        this.blobKey = blobKey;
    }

    public void setLabels(String newLabels) {
        // This may need to changed for further processing of how we should store labels // query them
        this.labels = newLabels;
    }

    public void setFromLang(String newLang) {
        this.fromLang = newLang;
    }
    
    public void setToLang(String newLang) {
        this.toLang = newLang;
    }

    public void setNewText(String newText) {
        this.textNotTranslated = newText;
    }

    public void setTextTranslated(String newText) {
        this.textTranslated = newText;
    }

    public void setCardKey(String cardKey) {
        this.cardKey = cardKey;
    }

    public Entity createEntity(Key folderKey) {
        
        Entity card = new Entity("Card", folderKey);

        // Store initial card entity without properties to generate auto ID for entity
        // This is necessary to obtain a "complete" key
        DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
        datastore.put(card);

        card.setProperty("blobKey", this.blobKey);
        card.setProperty("labels", this.labels);
        card.setProperty("fromLang", this.fromLang);
        card.setProperty("toLang", this.toLang);
        card.setProperty("textNotTranslated", this.textNotTranslated);
        card.setProperty("textTranslated", this.textTranslated);
        card.setProperty("cardKey", KeyFactory.keyToString(card.getKey()));

        return card;
    }

    public static Card EntityToCard(Entity entity) {

        String blobKey = (String) entity.getProperty("blobKey");
        String labels = (String) entity.getProperty("labels");
        String fromLang = (String) entity.getProperty("fromLang");
        String toLang = (String) entity.getProperty("toLang");
        String textNotTranslated = (String) entity.getProperty("textNotTranslated");
        String textTranslated = (String) entity.getProperty("textTranslated");
        String cardKey = (String) entity.getProperty("cardKey");

        Card card = new Card(blobKey, labels, fromLang, toLang, textNotTranslated, textTranslated);
        card.setCardKey(cardKey);
        
        return card;
    }
}
@@ -981,32 +981,531 @@
  %22null%22;%0A    %7D%0A%0A
+    public Card(Entity entity)%7B%0A%0A      this.blobKey = (String) entity.getProperty(%22blobKey%22);%0A      this.labels = (String) entity.getProperty(%22labels%22);%0A      this.fromLang = (String) entity.getProperty(%22fromLang%22);%0A      this.toLang = (String) entity.getProperty(%22toLang%22);%0A      this.textNotTranslated = (String) entity.getProperty(%22textNotTranslated%22);%0A      this.textTranslated = (String) entity.getProperty(%22textTranslated%22);%0A      this.cardKey = (String) entity.getProperty(%22cardKey%22);%0A    %7D%0A%0A
     public Strin
@@ -3540,719 +3540,6 @@
    %7D
-%0A%0A    public static Card EntityToCard(Entity entity) %7B%0A%0A        String blobKey = (String) entity.getProperty(%22blobKey%22);%0A        String labels = (String) entity.getProperty(%22labels%22);%0A        String fromLang = (String) entity.getProperty(%22fromLang%22);%0A        String toLang = (String) entity.getProperty(%22toLang%22);%0A        String textNotTranslated = (String) entity.getProperty(%22textNotTranslated%22);%0A        String textTranslated = (String) entity.getProperty(%22textTranslated%22);%0A        String cardKey = (String) entity.getProperty(%22cardKey%22);%0A%0A        Card card = new Card(blobKey, labels, fromLang, toLang, textNotTranslated, textTranslated);%0A        card.setCardKey(cardKey);%0A        %0A        return card;%0A    %7D
 %0A%7D

Add Card constructor that takes in an Entity