package it.integry.integrywmsnative.core.data_store.db.entity;

import androidx.room.ColumnInfo;
import androidx.room.Entity;
import androidx.room.ForeignKey;
import androidx.room.Ignore;
import androidx.room.Index;
import androidx.room.PrimaryKey;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.Date;

@Entity(
        tableName = "articoli_ordine",
        indices = {
                @Index(value = "id_ordine"),
                @Index(value = {"id_ordine", "cod_mart"}, unique = true)
        },
        foreignKeys = {
                @ForeignKey(
                        entity = Ordine.class,
                        parentColumns = "ordine_id",
                        childColumns = "id_ordine",
                        onDelete = ForeignKey.CASCADE
                )
        }
)
public class ArticoloOrdine {
    @PrimaryKey(autoGenerate = true)
    @ColumnInfo(name = "articolo_ordine_id")
    private Integer articoloOrdineId;

    @ColumnInfo(name = "cod_mart")
    private String codMart;

    @ColumnInfo(name = "descrizione")
    private String descrizione;

    @ColumnInfo(name = "unt_mis")
    private String untMis;

    @ColumnInfo(name = "qta_cnf")
    private float qtaCnf;

    @ColumnInfo(name = "bar_code")
    private String barCode;

    @ColumnInfo(name = "merce_da_ric")
    private float merceDaRic;

    @ColumnInfo(name = "media_sett")
    private float mediaSett;

    @ColumnInfo(name = "flag_qta_multipla")
    private String flagQtaMultipla;

    @ColumnInfo(name = "qta_min_ordinabile")
    private float qtaMinOrdinabile;

    @ColumnInfo(name = "id_ordine")
    private int idOrdine;

    @ColumnInfo(name = "qta_ord")
    private float qtaOrd;

    @ColumnInfo(name = "data_ins")
    private Date dataIns;

    @ColumnInfo(name = "new_no_promo")
    private boolean newNoPromo;

    @ColumnInfo(name = "system_note")
    private String systemNote;

    @Ignore
    private int ggScadenza = 0;

    @Ignore
    private BigDecimal giacenza = BigDecimal.ZERO;

    @Ignore
    private BigDecimal qtaPrevistaVendita = BigDecimal.ZERO;

    @Ignore
    private BigDecimal qtaProposta = BigDecimal.ZERO;

    @Ignore
    private int bgTint;


    public Integer getArticoloOrdineId() {
        return articoloOrdineId;
    }

    public void setArticoloOrdineId(Integer articoloOrdineId) {
        this.articoloOrdineId = articoloOrdineId;
    }

    public String getCodMart() {
        return codMart;
    }

    public void setCodMart(String codMart) {
        this.codMart = codMart;
    }

    public String getDescrizione() {
        return descrizione;
    }

    public void setDescrizione(String descrizione) {
        this.descrizione = descrizione;
    }

    public String getUntMis() {
        return untMis;
    }

    public void setUntMis(String untMis) {
        this.untMis = untMis;
    }

    public float getQtaCnf() {
        return qtaCnf;
    }

    public void setQtaCnf(float qtaCnf) {
        this.qtaCnf = qtaCnf;
    }

    public String getBarCode() {
        return barCode;
    }

    public void setBarCode(String barCode) {
        this.barCode = barCode;
    }

    public float getMerceDaRic() {
        return merceDaRic;
    }

    public void setMerceDaRic(float merceDaRic) {
        this.merceDaRic = merceDaRic;
    }

    public float getMediaSett() {
        return mediaSett;
    }

    public void setMediaSett(float mediaSett) {
        this.mediaSett = mediaSett;
    }

    public String getFlagQtaMultipla() {
        return flagQtaMultipla;
    }

    public void setFlagQtaMultipla(String flagQtaMultipla) {
        this.flagQtaMultipla = flagQtaMultipla;
    }

    public float getQtaMinOrdinabile() {
        return qtaMinOrdinabile;
    }

    public void setQtaMinOrdinabile(float qtaMinOrdinabile) {
        this.qtaMinOrdinabile = qtaMinOrdinabile;
    }

    public int getIdOrdine() {
        return idOrdine;
    }

    public void setIdOrdine(int idOrdine) {
        this.idOrdine = idOrdine;
    }

    public float getQtaOrd() {
        return qtaOrd;
    }

    public void setQtaOrd(float qtaOrd) {
        this.qtaOrd = qtaOrd;
    }

    public Date getDataIns() {
        return dataIns;
    }

    public void setDataIns(Date dataIns) {
        this.dataIns = dataIns;
    }

    public int getGgScadenza() {
        return ggScadenza;
    }

    public ArticoloOrdine setGgScadenza(int ggScadenza) {
        this.ggScadenza = ggScadenza;
        return this;
    }

    public BigDecimal getGiacenza() {
        return giacenza;
    }

    public ArticoloOrdine setGiacenza(BigDecimal giacenza) {
        this.giacenza = giacenza;
        return this;
    }

    public BigDecimal getQtaPrevistaVendita() {
        return qtaPrevistaVendita;
    }

    public ArticoloOrdine setQtaPrevistaVendita(BigDecimal qtaPrevistaVendita) {
        this.qtaPrevistaVendita = qtaPrevistaVendita;
        return this;
    }

    public BigDecimal getQtaProposta() {
        return qtaProposta;
    }

    public ArticoloOrdine setQtaProposta(BigDecimal qtaProposta) {
        this.qtaProposta = qtaProposta;
        return this;
    }

    public BigDecimal getQtaCnfPrevistaVendita() {
        return qtaPrevistaVendita.equals(BigDecimal.ZERO) ? BigDecimal.ZERO : qtaPrevistaVendita.divide(BigDecimal.valueOf(qtaCnf), 0, RoundingMode.CEILING);
    }

    public BigDecimal getQtaCnfProposta() {
        return qtaProposta.equals(BigDecimal.ZERO) ? BigDecimal.ZERO : qtaProposta.divide(BigDecimal.valueOf(qtaCnf), 0, RoundingMode.CEILING);
    }

    public BigDecimal getQtaCnfGiacenza() {
        return giacenza.equals(BigDecimal.ZERO) ? BigDecimal.ZERO : giacenza.divide(BigDecimal.valueOf(qtaCnf), 0, RoundingMode.CEILING);
    }

    public boolean isNewNoPromo() {
        return newNoPromo;
    }

    public void setNewNoPromo(boolean newNoPromo) {
        this.newNoPromo = newNoPromo;
    }

    public float getCnfDaRic() {
        return qtaCnf > 0.f ? merceDaRic * qtaCnf : 0;
    }

    public int checkQtaProposta() {
        int check = 0;
        if (qtaProposta != null && !qtaProposta.equals(BigDecimal.ZERO)) {
            check = BigDecimal.valueOf(qtaOrd).compareTo(qtaProposta);
        }
        return check;
    }

    public int getBgTint() {
        return bgTint;
    }

    public ArticoloOrdine setBgTint(int bgTint) {
        this.bgTint = bgTint;
        return this;
    }

    public String getSystemNote() {
        return systemNote;
    }

    public void setSystemNote(String systemNote) {
        this.systemNote = systemNote;
    }
}
