/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package common;

import java.util.Map;
import java.util.regex.Pattern;

/**
 *
 * @author Eugenio
 * @param
 */
public class Instruction extends RegistroBase {

    public Instruction(String type, String inst) {
        super.Set("type", type).Set("inst", inst);
    }

    public Instruction(String inst) {
        super.Set("inst", inst);
    }

    public Instruction() {
    }

    @Override
    public Instruction Set(String attrib, Boolean valor) {
        super.Set(attrib, valor); //To change body of generated methods, choose Tools | Templates.
        return this;
    }

    @Override
    public Instruction Set(String attrib, int valor) {
        super.Set(attrib, valor); //To change body of generated methods, choose Tools | Templates.
        return this;
    }

    @Override
    public Instruction Set(String attrib, Long valor) {
        super.Set(attrib, valor); //To change body of generated methods, choose Tools | Templates.
        return this;
    }

    @Override
    public Instruction Set(String attrib, String value) {
        super.Set(attrib, value); //To change body of generated methods, choose Tools | Templates.
        return this;
    }

//    public Instruction S(String attrib, int value) {
//        super.Set(attrib, value); //To change body of generated methods, choose Tools | Templates.
//        return this;
//    }
    @Override
    public String toString() {
        return "Instruction {\n" + super.toString() + "\n}\n"; //To change body of generated methods, choose Tools | Templates.
    }

    @Override
    public Instruction R(String attributes) {
        super.R(attributes); //To change body of generated methods, choose Tools | Templates.
        return this;
    }

    public boolean igual(String atributo, Instruction r2) {
        return eq(atributo, r2.Get(atributo));
    }

    public boolean maior(String atributo, int i) throws Exception {
        if (!isNumber(atributo)) {
            return false;
        }
        return GetInt(atributo) > i;
    }

    public boolean menor(String atributo, int i) throws Exception {
        if (!isNumber(atributo)) {
            return false;
        }
        return GetInt(atributo) < i;
    }

    public int getInt(String indice, int val) {
        try {
            return GetInt(indice);
        } catch (Exception e) {
            return val;
        }
    }

    public Instruction copy(String atrib, Instruction r) {
        super.copy(atrib, r);
        return this;
    }

    public Instruction Merge(String attrs, Instruction src) {
        if (src != null) {
            for (String a : attrs.split(",")) {
                if (!this.Has(a) && src.Has(a)) {
                    this.Set(a, src.Get(a));
                }
            }
        }
        return this;
    }

    public void Prepend(String attrib, String p) {
        if (Has(attrib)) {
            Instruction.this.Set(attrib, p + Get(attrib));
        }
    }

    public void Append(String attrib, String p) {
        if (Has(attrib)) {
            Instruction.this.Set(attrib, Get(attrib) + p);
        }
    }

    public Instruction copy() {
        Instruction rb = new Instruction();
        for (Map.Entry<String, String> entry : attr.entrySet()) {
            rb.Set(entry.getKey(), entry.getValue());
        }
        return rb;
    }

    protected static Pattern NUMBER_EXPR = Pattern.compile("-?\\d+(\\.\\d+)?");

    public boolean IsValue(String attr) {
        return NUMBER_EXPR.matcher(Get(attr)).matches();
    }

}
