package de.dhbwstuttgart.typeinference.result;

import de.dhbwstuttgart.server.packet.dataContainers.ISerializableData;
import de.dhbwstuttgart.server.packet.dataContainers.KeyStorage;
import de.dhbwstuttgart.server.packet.dataContainers.serialized.SerialMap;
import de.dhbwstuttgart.server.packet.dataContainers.serialized.SerialValue;
import de.dhbwstuttgart.syntaxtree.type.RefTypeOrTPHOrWildcardOrGeneric;
import de.dhbwstuttgart.typeinference.unify.UnifyContext;

/**
 * Paare, welche das Unifikationsergebnis darstellen
 */
public abstract class ResultPair<A extends RefTypeOrTPHOrWildcardOrGeneric,B extends RefTypeOrTPHOrWildcardOrGeneric>
  implements Comparable<ResultPair<A,B>>, ISerializableData {
    private final A left;
    private final B right;

    public abstract void accept(ResultPairVisitor visitor);

    public ResultPair(A left, B right){
        this.left = left;
        this.right = right;
    }

    public A getLeft() {
        return left;
    }

    public B getRight() {
        return right;
    }
    
    public String toString() {
    	return "(" + left.toString() + ", " + right.toString() + ")";
    }

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((left == null) ? 0 : left.getOffset().hashCode());
		result = prime * result + ((right == null) ? 0 : right.getOffset().hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj)
			return true;
		if (obj == null)
			return false;
		if (getClass() != obj.getClass())
			return false;
		ResultPair<?,?> other = (ResultPair<?,?>) obj;
		if (left == null) {
			if (other.left != null)
				return false;
		} else if (!left.getOffset().equals(other.left.getOffset()))
			return false;
		if (right == null) {
			if (other.right != null)
				return false;
		} else if (!right.getOffset().equals(other.right.getOffset()))
			return false;
		return true;
	}

	@Override
	public int compareTo(ResultPair<A, B> o) {
		if (o == null) {
			return 1; // this is greater than null
		}

		return o.left.toString().compareTo(this.left.toString());
	}



	@Override
	public SerialMap toSerial(KeyStorage keyStorage) {
		SerialMap serialized = new SerialMap();
		String type = switch (this) {
      case PairNoResult _ -> "pnr";
      case PairTPHEqualTPH _ -> "ptet";
      case PairTPHsmallerTPH _ -> "ptst";
      case PairTPHequalRefTypeOrWildcardType _ -> "ptertwt";
      default -> throw new RuntimeException("No type defined for ResultPair of class " + this.getClass().getName());
    };
    serialized.put("type", type);
		// we only insert null for the object and expect the child classes to call this and override the value with themselves
		serialized.put("object", SerialValue.NULL);
		return serialized;
	}

	public static <A extends RefTypeOrTPHOrWildcardOrGeneric,B extends RefTypeOrTPHOrWildcardOrGeneric> ResultPair<A,B>
	fromSerial(SerialMap data, UnifyContext context) {
		String type = data.getValue("type").getOf(String.class);
		SerialMap object = data.getMap("object");

    return switch (type) {
      case "pnr" -> (ResultPair) PairNoResult.fromSerial2(object, context);
      case "ptet" -> (ResultPair) PairTPHEqualTPH.fromSerial2(object, context);
      case "ptst" -> (ResultPair) PairTPHsmallerTPH.fromSerial2(object, context);
      case "ptertwt" -> (ResultPair) PairTPHequalRefTypeOrWildcardType.fromSerial2(object, context);
      default -> throw new RuntimeException("Could not unserialize class of unhandled type " + type);
    };
	}
}
