package com.reallinkbilling.add;

import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.utils.EWayBillAccess;

public class GenerateEWayBill extends ActionSupport implements SessionAware {
    private static final long serialVersionUID = 1L;

    private SessionMap<String, Object> sessionMap;

    private String billNumber;
    private String billDateTime;
    private String billType;
    private String customerName;
    private String customerMobileNumber;
    private String totalPaybleAmount;
    private String transportCharge;
    private String ewbDistanceKm;
    private String ewbTransportMode;
    private String vehicleNumberEW;

    @Override
    public String execute() throws Exception {
        if (!EWayBillAccess.isEnabled(sessionMap)) {
            addActionError("E-Way Bill is disabled for this shop.");
            return ERROR;
        }

        HttpServletResponse resp = ServletActionContext.getResponse();
        resp.setContentType("text/html;charset=UTF-8");
        PrintWriter out = resp.getWriter();
        String now = new SimpleDateFormat("dd-MM-yyyy HH:mm:ss").format(new Date());

        String modeText = "";
        if ("1".equals(ewbTransportMode)) modeText = "Road";
        else if ("2".equals(ewbTransportMode)) modeText = "Rail";
        else if ("3".equals(ewbTransportMode)) modeText = "Air";
        else if ("4".equals(ewbTransportMode)) modeText = "Ship";

        out.println("<html><head><title>E-Way Bill (Demo)</title>");
        out.println("<meta charset='utf-8'/><style>body{font-family:Arial;margin:20px;} table{border-collapse:collapse;width:100%;} td,th{border:1px solid #ccc;padding:6px;} h2{margin:0 0 10px 0;} .note{color:#c00;font-weight:bold;}</style></head><body>");
        out.println("<h2>E-Way Bill - Preview (Demo)</h2>");
        out.println("<div class='note'>This is a placeholder. Wire to NIC/GSP per docs once keys are configured.</div>");
        out.println("<p>Generated at: " + now + "</p>");
        out.println("<table>");
        out.println(row("Bill Number", billNumber));
        out.println(row("Bill Date", billDateTime));
        out.println(row("Bill Type", billType));
        out.println(row("Customer", customerName));
        out.println(row("Mobile", customerMobileNumber));
        out.println(row("Total Amount", safe(totalPaybleAmount)));
        out.println(row("Transport Charges", safe(transportCharge)));
        out.println(row("Distance (KM)", safe(ewbDistanceKm)));
        out.println(row("Transport Mode", modeText));
        out.println(row("Vehicle Number", safe(vehicleNumberEW)));
        out.println("</table>");
        out.println("</body></html>");
        out.flush();
        return NONE;
    }

    private static String safe(String v){ return v==null?"":v; }
    private static String row(String k, String v){ return "<tr><th>"+k+"</th><td>"+safe(v)+"</td></tr>"; }

    public void setSession(Map<String, Object> map) { sessionMap = (SessionMap<String, Object>) map; }

    public String getBillNumber() { return billNumber; }
    public void setBillNumber(String billNumber) { this.billNumber = billNumber; }
    public String getBillDateTime() { return billDateTime; }
    public void setBillDateTime(String billDateTime) { this.billDateTime = billDateTime; }
    public String getBillType() { return billType; }
    public void setBillType(String billType) { this.billType = billType; }
    public String getCustomerName() { return customerName; }
    public void setCustomerName(String customerName) { this.customerName = customerName; }
    public String getCustomerMobileNumber() { return customerMobileNumber; }
    public void setCustomerMobileNumber(String customerMobileNumber) { this.customerMobileNumber = customerMobileNumber; }
    public String getTotalPaybleAmount() { return totalPaybleAmount; }
    public void setTotalPaybleAmount(String totalPaybleAmount) { this.totalPaybleAmount = totalPaybleAmount; }
    public String getTransportCharge() { return transportCharge; }
    public void setTransportCharge(String transportCharge) { this.transportCharge = transportCharge; }
    public String getEwbDistanceKm() { return ewbDistanceKm; }
    public void setEwbDistanceKm(String ewbDistanceKm) { this.ewbDistanceKm = ewbDistanceKm; }
    public String getEwbTransportMode() { return ewbTransportMode; }
    public void setEwbTransportMode(String ewbTransportMode) { this.ewbTransportMode = ewbTransportMode; }
    public String getVehicleNumberEW() { return vehicleNumberEW; }
    public void setVehicleNumberEW(String vehicleNumberEW) { this.vehicleNumberEW = vehicleNumberEW; }
}


