package nc.ui.pubapp.uif2app.actions;

import java.awt.event.ActionEvent;

import nc.bs.uif2.IActionCode;
import nc.ui.pub.bill.action.BillTableLineAction;
import nc.ui.pubapp.uif2app.actions.intf.ICardPanelDefaultActionProcessor;
import nc.ui.pubapp.uif2app.event.card.BodyRowEditType;
import nc.ui.pubapp.uif2app.event.card.CardBodyAfterRowEditEvent;
import nc.ui.uif2.NCAction;
import nc.ui.uif2.actions.ActionInitializer;

public class BodyPasteToTailAction extends AbstractBodyPasteLineAction
    implements ICardPanelDefaultActionProcessor {
  private static final long serialVersionUID = -3045319122603006824L;

  public BodyPasteToTailAction() {
    this.setCode(IActionCode.PASTELINETOTAIL);
    NCAction action = new NCAction() {
      private static final long serialVersionUID = 1L;

      @Override
      public void doAction(ActionEvent e) throws Exception {
        // do nothing
      }
    };
    ActionInitializer.initializeAction(action, IActionCode.PASTELINETOTAIL);
    this.putActionValue(action);
  }

  @Override
  public void doAction() {
    boolean doOperateFlag =
        this.getCardPanel().doLineAction(
            super.getCardPanel().getCurrentBodyTableCode(),
            BillTableLineAction.PASTELINETOTAIL);
    if (doOperateFlag) {
      super.doAction();
      this.fireEvent();
    }
  }
  
  private void fireEvent() {
    int rowCount = this.getCardPanel().getBillModel().getRowCount();
    int pasteLineNumer = this.getCardPanel().getBillModel().getPasteLineNumer();
    if(pasteLineNumer > 0) {
      int[] rows = new int[pasteLineNumer];
      for (int i = 0; i < pasteLineNumer; i++) {
        rows[i] = rowCount - pasteLineNumer + i;
      }
      if(this.getCardPanel().getHeadItem("pk_billtype") != null) {
	      Object  pk_billtype = this.getCardPanel().getHeadItem("pk_billtype").getValueObject();
	      //2023/10/11 YY
	      if(!"F1".equals(pk_billtype)) {
	          this.getModel().fireEvent(
	                  new CardBodyAfterRowEditEvent(this.getCardPanel(),
	                      BodyRowEditType.PASTELINETOTAIL, rows));
	      }
      }

    }
  }

  @Override
  public int getType() {
    return BillTableLineAction.PASTELINETOTAIL;
  }

  @Override
  protected int lastPastedRow() {
    return this.getCardPanel().getBodyPanel().getTable().getRowCount() - 1;
  }
}
