package nl.grauw.glass.instructions;

import nl.grauw.glass.Scope;
import nl.grauw.glass.expressions.Expression;
import nl.grauw.glass.expressions.Register;
import nl.grauw.glass.expressions.Schema;

public class Cp extends InstructionFactory {

    @Override
    public InstructionObject createObject(Scope context, Expression arguments) {
        if (Cp_R.ARGUMENTS.check(arguments)) {
            return new Cp_R(context, arguments);
        }
        if (Cp_N.ARGUMENTS.check(arguments)) {
            return new Cp_N(context, arguments);
        }
        throw new ArgumentException();
    }

    public static class Cp_R extends InstructionObject {

        public static Schema ARGUMENTS = new Schema(Schema.DIRECT_R_INDIRECT_HL_IX_IY);

        private Expression argument;

        public Cp_R(Scope context, Expression arguments) {
            super(context);
            this.argument = arguments;
        }

        @Override
        public int getSize() {
            return indexifyIndirect(argument.getRegister(), 1);
        }

        @Override
        public byte[] getBytes() {
            Register register = argument.getRegister();
            return indexifyIndirect(register, (byte) (0xB8 | register.get8BitCode()));
        }

    }

    public static class Cp_N extends InstructionObject {

        public static Schema ARGUMENTS = new Schema(Schema.DIRECT_N);

        private Expression argument;

        public Cp_N(Scope context, Expression arguments) {
            super(context);
            this.argument = arguments;
        }

        @Override
        public int getSize() {
            return 2;
        }

        @Override
        public byte[] getBytes() {
            return new byte[] {
                (byte) 0xFE, (byte) argument.getInteger()
            };
        }

    }

}
