package gitlet;

import java.io.File;
import static gitlet.Utils.*;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// TODO: any imports you need here
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.util.Locale;
import java.io.Serializable;
import java.util.Date;
import java.time.LocalDateTime;
import java.util.HashMap;


/** Represents a gitlet commit object.
 *
 *  does at a high level.
 *
 *  @author Tianye Meng
 */
public class Commit implements Serializable {
    /**
     * uid: a unique string that represents the current commit
     * Uid is also the name of a commit file.
     *
     * timestamp: the time when a commit is created.
     * parent1 & parent2: represents the 2 parents of current commit. Parent 1 is the default parent.
     * blobs: a hashmap that represents pointers of files contained in this current commit
     *
     * List all instance variables of the Commit class here with a useful
     * comment above them describing what that variable represents and how that
     * variable is used. We've provided one example for `message`.
     */

    /** The message of this Commit. */
    private String message;
    private String uid;
    private String timeStamp;
    private Commit parent1;
    private Commit parent2;
    private HashMap<String, String> blobs;
    private final DateFormat dateFormat = new SimpleDateFormat("EEE MMM d HH:mm:ss yyyy Z", Locale.ENGLISH);

    public Commit (){
        message = "initial commit";
        uid = Utils.sha1(Utils.serialize(this));
        timeStamp = "Thu Jan 1 00:00:00 1970 -0800";
        parent1 = null;
        parent2 = null;
        blobs = new HashMap<>();//key = sha1, value = file name
    }
    public Commit (String msg, HashMap blob, Commit p1, Commit p2){
        message = msg;
        parent1 = p1;
        blobs = blob;
        parent2 = p2;
        LocalDateTime now = LocalDateTime.now();
        this.timeStamp = dateFormat.format(new java.util.Date());
        this.uid = gitlet.Utils.sha1(gitlet.Utils.serialize(this));
    }

    public String getTimeStamp() {
        return timeStamp;
    }
    public String getUid(){
        return uid;
    }
    public String getMessage(){
        return message;
    }
    public Commit getParent1(){
        return parent1;
    }
    public Commit getParent2(){
        return parent2;
    }
    public HashMap<String, String> getBlobs(){
        return blobs;
    }
    public String getOneBlob(String key){
        return blobs.get(key);
    }
    /*
    check if this commit has a blob whose sha1 value is sha1val.
     */
    public boolean contains (String sha1val){
        if(getBlobs().containsKey(sha1val)){
            return true;
        }
        return false;
    }
    /*
    pass in a file and add this file to the blobs of current commit.
    A blob's key is it's sha1 value and the corresponding value is

     */
    public void setBlobs(File f, String name){
        blobs.put(Utils.sha1(serialize(f)), name);
    }

    /* TODO: fill in the rest of this class. */
}
