package com.utopiaindustries.dao.uind;

import com.utopiaindustries.model.uind.PurchaseOrder;
import org.springframework.jdbc.core.RowMapper;

import java.sql.ResultSet;
import java.sql.SQLException;

public class PurchaseOrderRowMapper implements RowMapper<PurchaseOrder> {

    @Override
    public PurchaseOrder mapRow(ResultSet rs, int i) throws SQLException {
        PurchaseOrder purchaseOrder = new PurchaseOrder();
        purchaseOrder.setId( rs.getLong( "id" ) );
        purchaseOrder.setCode( rs.getString( "code" ) );
        purchaseOrder.setDateGenerated( rs.getTimestamp( "date_generated" ).toLocalDateTime() );
        if ( rs.getDate( "physical_date" ) != null ) {
            purchaseOrder.setPhysicalDate( rs.getDate( "physical_date" ).toLocalDate() );
        }
        if ( rs.getTimestamp( "approval_date" ) != null ) {
            purchaseOrder.setApprovalDate( rs.getTimestamp( "approval_date" ).toLocalDateTime() );
        }
        if ( rs.getTimestamp( "indenter_div_approval_date" ) != null ) {
            purchaseOrder.setIndentDivApprovalDate( rs.getTimestamp( "indenter_div_approval_date" ).toLocalDateTime() );
        }
        purchaseOrder.setGeneratedByUser( rs.getString( "generated_by_user" ) );
        purchaseOrder.setApprovedByUser( rs.getString( "approved_by_user" ) );
        purchaseOrder.setIndenterDivApprovedByUser( rs.getString( "indenter_div_approved_by" ) );
        purchaseOrder.setCompanyId( rs.getLong( "company_id" ) );
        purchaseOrder.setFunctionId( rs.getLong( "function_id" ) );
        purchaseOrder.setDepartmentId( rs.getLong( "department_id" ) );
        purchaseOrder.setSectionId( rs.getLong( "section_id" ) );
        purchaseOrder.setQuotationReferenceNumber( rs.getString( "quotation_reference_number" ) );
        purchaseOrder.setQuotationDate( rs.getDate( "quotation_date" ).toLocalDate() );
        purchaseOrder.setCurrencySymbol( rs.getString( "currency_symbol" ) );
        Float currencyConversionRate = rs.getFloat( "currency_conversion_rate" );
        if ( rs.wasNull() ) {
            currencyConversionRate = null;
        }
        purchaseOrder.setCurrencyConversionRate( currencyConversionRate );
        purchaseOrder.setGstAmount( rs.getFloat( "gst_amount" ) );
        purchaseOrder.setGstAmountInPkr( rs.getFloat( "gst_amount_in_pkr" ) );
        purchaseOrder.setTotalCost( rs.getFloat( "total_cost" ) );
        purchaseOrder.setTotalCostInPkr( rs.getFloat( "total_cost_in_pkr" ) );
        purchaseOrder.setTotalCostInWords( rs.getString( "total_cost_in_words" ) );
        purchaseOrder.setTotalCostInWordsInPkr( rs.getString( "total_cost_in_words_in_pkr" ) );
        purchaseOrder.setOutstandingAdvanceAmount( rs.getFloat( "outstanding_advance_amount" ) );
        purchaseOrder.setAmountPaid( rs.getFloat( "amount_paid" ) );
        purchaseOrder.setSupplierId( rs.getLong( "supplier_id" ) );
        Long brokerId = rs.getLong( "broker_id" );
        if ( rs.wasNull() ) {
            brokerId = null;
        }
        purchaseOrder.setBrokerId( brokerId );
        Long deliverySiteId = rs.getLong( "delivery_location_site_id" );
        if ( rs.wasNull() ) {
            deliverySiteId = null;
        }
        purchaseOrder.setDeliveryLocationSiteId( deliverySiteId );
        purchaseOrder.setUserStatus( rs.getString( "user_status" ) );
        purchaseOrder.setUserRemarks( rs.getString( "user_remarks" ) );
        Long mainPaymentTermId = rs.getLong( "main_payment_term_id" );
        if ( rs.wasNull() ) {
            mainPaymentTermId = null;
        }
        purchaseOrder.setMainPaymentTermId( mainPaymentTermId );
        purchaseOrder.setShipmentMode( rs.getString( "shipment_mode" ) );
        purchaseOrder.setManagementStatus( rs.getString( "management_status" ) );
        purchaseOrder.setManagementRemarks( rs.getString( "management_remarks" ) );
        purchaseOrder.setIndenterDivPoStatus( rs.getString( "indenter_div_head_status" ) );
        purchaseOrder.setIndenterDivPoRemarks( rs.getString( "indenter_div_head_remarks" ) );
        purchaseOrder.setCompleted( rs.getBoolean( "completed" ) );
        if ( rs.getTimestamp( "date_completed" ) != null ) {
            purchaseOrder.setDateCompleted( rs.getTimestamp( "date_completed" ).toLocalDateTime() );
        }
        purchaseOrder.setTerminationReason( rs.getString( "termination_reason" ) );
        purchaseOrder.setParentDocumentType( rs.getString( "parent_document_type" ) );
        purchaseOrder.setParentDocumentCode( rs.getString( "parent_document_code" ) );
        Long parentDocumentId = rs.getLong( "parent_document_id" );
        if ( rs.wasNull() ) {
            parentDocumentId = null;
        }
        purchaseOrder.setParentDocumentId( parentDocumentId );
        Long indentId = rs.getLong( "indent_id" );
        if ( rs.wasNull() ) {
            indentId = null;
        }
        purchaseOrder.setIndentId( indentId );
        purchaseOrder.setIsLocked( rs.getBoolean( "is_locked" ) );
        purchaseOrder.setAreChargesPaid( rs.getBoolean( "are_charges_paid" ) );
        purchaseOrder.setIsFlagged( rs.getBoolean( "is_flagged" ) );
        return purchaseOrder;
    }
}
