#include "ns3/applications-module.h"
#include "ns3/bridge-module.h"
#include "ns3/core-module.h"
#include "ns3/csma-module.h"
#include "ns3/error-model.h"
#include "ns3/internet-module.h"
#include "ns3/netanim-module.h"
#include "ns3/network-module.h"

#include <iomanip>
#include <iostream>
#include <string>

using namespace ns3;

int main(int argc, char* argv[]) {
    std::string scenario = "01A";
    std::string state = "before";
    CommandLine cmd(__FILE__);
    cmd.AddValue("case", "01A disconnected, 01B frame loss, or 01C bottleneck", scenario);
    cmd.AddValue("state", "before or after", state);
    cmd.Parse(argc, argv);
    if ((scenario != "01A" && scenario != "01B" && scenario != "01C") ||
        (state != "before" && state != "after")) {
        std::cerr << "Use --case=01A|01B|01C and --state=before|after\n";
        return 2;
    }
    const bool before = state == "before";
    const std::string prefix = scenario + "-" + state;
    NodeContainer nodes;
    nodes.Create(3);
    const auto source = nodes.Get(0);
    const auto bridge = nodes.Get(1);
    const auto sink = nodes.Get(2);
    CsmaHelper wall;
    wall.SetChannelAttribute("DataRate", StringValue("100Mbps"));
    wall.SetChannelAttribute("Delay", TimeValue(MicroSeconds(4)));
    CsmaHelper rack;
    rack.SetChannelAttribute("DataRate", StringValue(scenario == "01C" && before ? "256kbps" : "100Mbps"));
    rack.SetChannelAttribute("Delay", TimeValue(MicroSeconds(4)));
    const auto wallPorts = wall.Install(NodeContainer(source, bridge));
    const auto rackPorts = rack.Install(NodeContainer(bridge,sink));
    BridgeHelper bridgeHelper;
    bridgeHelper.Install(bridge, NetDeviceContainer(wallPorts.Get(1),rackPorts.Get(0)));
    InternetStackHelper stack;
    stack.Install(NodeContainer(source,sink));
    Ipv4AddressHelper addresses;
    addresses.SetBase("192.168.10.0","255.255.255.0");
    const auto ip = addresses.Assign(NetDeviceContainer(wallPorts.Get(0),rackPorts.Get(1)));
    if (scenario == "01A" && before) {
        auto port = DynamicCast<CsmaNetDevice>(rackPorts.Get(1));
        port->SetSendEnable(false);
        port->SetReceiveEnable(false);
    }
    if (scenario == "01B" && before) {
        auto errors = CreateObject<RateErrorModel>();
        errors->SetAttribute("ErrorUnit", EnumValue<RateErrorModel::ErrorUnit>(RateErrorModel::ERROR_UNIT_PACKET));
        errors->SetAttribute("ErrorRate", DoubleValue(0.28));
        errors->AssignStreams(27);
        DynamicCast<CsmaNetDevice>(rackPorts.Get(1))->SetReceiveErrorModel(errors);
    }
    const uint16_t port = 4101;
    const uint32_t totalSent = scenario == "01C" ? 500 : 80;
    UdpServerHelper server(port);
    const auto serverApp = server.Install(sink);
    serverApp.Start(Seconds(0));
    UdpClientHelper client(ip.GetAddress(1),port);
    client.SetAttribute("MaxPackets", UintegerValue(totalSent));
    client.SetAttribute("Interval", TimeValue(scenario == "01C" ? MilliSeconds(3) : MilliSeconds(20)));
    client.SetAttribute("PacketSize", UintegerValue(900));
    const auto clientApp = client.Install(source);
    clientApp.Start(Seconds(0.5));
    rack.EnablePcap(prefix+"-rack.pcap",rackPorts.Get(1),false,true);
    wall.EnablePcap(prefix+"-wall.pcap",wallPorts.Get(0),false,true);
    AnimationInterface::SetConstantPosition(source,10,16);
    AnimationInterface::SetConstantPosition(bridge,40,16);
    AnimationInterface::SetConstantPosition(sink,70,16);
    AnimationInterface anim(prefix+".xml");
    anim.UpdateNodeDescription(source,"LAB-07 / wall jack");
    anim.UpdateNodeDescription(bridge,"Rack switching path");
    anim.UpdateNodeDescription(sink,"Gateway test endpoint");
    Simulator::Stop(Seconds(scenario == "01C" ? 4 : 3));
    Simulator::Run();
    const auto delivered = DynamicCast<UdpServer>(serverApp.Get(0))->GetReceived();
    std::cout << prefix << " planned=" << totalSent << " delivered=" << delivered
              << " delivery_percent=" << std::fixed << std::setprecision(2)
              << (100.0 * delivered / totalSent) << '\n';
    Simulator::Destroy();
}
