package de.tudarmstadt.informatik.hostage.protocol;


import org.alfresco.config.ConfigElement;
import org.alfresco.jlan.debug.ConsoleDebug;
import org.alfresco.jlan.debug.DebugConfigSection;
import org.alfresco.jlan.server.SessionListener;
import org.alfresco.jlan.server.SrvSession;
import org.alfresco.jlan.server.auth.CifsAuthenticator;
import org.alfresco.jlan.server.auth.DefaultAuthenticator;
import org.alfresco.jlan.server.config.CoreServerConfigSection;
import org.alfresco.jlan.server.config.GlobalConfigSection;
import org.alfresco.jlan.server.config.InvalidConfigurationException;
import org.alfresco.jlan.server.config.SecurityConfigSection;
import org.alfresco.jlan.server.config.ServerConfiguration;
import org.alfresco.jlan.server.core.DeviceContextException;
import org.alfresco.jlan.server.filesys.DiskDeviceContext;
import org.alfresco.jlan.server.filesys.DiskInterface;
import org.alfresco.jlan.server.filesys.DiskSharedDevice;
import org.alfresco.jlan.server.filesys.FileListener;
import org.alfresco.jlan.server.filesys.FilesystemsConfigSection;
import org.alfresco.jlan.server.filesys.NetworkFile;
import org.alfresco.jlan.smb.server.CIFSConfigSection;
import org.alfresco.jlan.smb.server.SMBServer;

import java.io.IOException;
import java.util.List;

import de.tudarmstadt.informatik.hostage.Handler;
import de.tudarmstadt.informatik.hostage.protocol.cifs.PseudoCIFSDiskDriver;
import de.tudarmstadt.informatik.hostage.wrapper.Packet;

public class CIFS implements Protocol, SessionListener, FileListener {
    private Handler mHandler;
    private SMBServer mSmbServer;

    public void initialize(Handler mHandler) {
        this.mHandler = mHandler;

        DiskInterface disk = new PseudoCIFSDiskDriver(mHandler);
        DiskDeviceContext context = null;
        try {
            context = (DiskDeviceContext) disk.createContext("theContext", new ConfigElement("",""));
        } catch (DeviceContextException e) {
            e.printStackTrace();
        }

        ServerConfiguration config = new ServerConfiguration("HostageConfig");
        CIFSConfigSection cifsConfig = new CIFSConfigSection(config);
        try {
            cifsConfig.setHostAnnouncer(false);
            cifsConfig.setNetBIOSSMB(true);
            cifsConfig.setWin32NetBIOS(false);
            cifsConfig.setAuthenticator(DefaultAuthenticator.class.getCanonicalName(), new ConfigElement("", ""), 0x10000000, true);
            cifsConfig.setTcpipSMB(true);
            cifsConfig.setNetBIOSDebug(true);
            cifsConfig.setTcpipSMBPort(28582);
            cifsConfig.setNameServerPort(28274);
            cifsConfig.setDatagramPort(28275);
            cifsConfig.setSessionPort(28276);
            cifsConfig.setServerName("HAHAHA");
            cifsConfig.setDomainName("Workgroup");

            FilesystemsConfigSection filesystems = new FilesystemsConfigSection(config);
            DiskSharedDevice dev = new DiskSharedDevice("VirtualDisk", disk, context);
            filesystems.addShare(dev);

            GlobalConfigSection global = new GlobalConfigSection(config);

            CoreServerConfigSection core = new CoreServerConfigSection(config);
            core.setThreadPool(2, 8);

            DebugConfigSection debugConfigSection = new DebugConfigSection(config);
            debugConfigSection.setDebug(ConsoleDebug.class.getCanonicalName(), new ConfigElement("append", ""));
            
            this.mSmbServer = new SMBServer(config);
            this.mSmbServer.addSessionListener(this);
            this.mSmbServer.addFileListener(this);
            this.mSmbServer.startServer();

        } catch (InvalidConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getPort() {
        return 1025;
    }

    @Override
    public boolean isClosed() {
        return false;
    }

    @Override
    public boolean isSecure() {
        return false;
    }

    @Override
    public List<Packet> processMessage(Packet message) {
        return null;
    }

    @Override
    public TALK_FIRST whoTalksFirst() {
        return null;
    }

    public String toString(){
        return "CIFS";
    }

    @Override
    public void sessionClosed(SrvSession srvSession) {
        System.out.println("---------------------- [CIFS] Session closed!");
    }

    @Override
    public void sessionCreated(SrvSession srvSession) {
        System.out.println("---------------------- [CIFS] Session created!");
    }

    @Override
    public void sessionLoggedOn(SrvSession srvSession) {
        System.out.println("---------------------- [CIFS] Session LoggedOn!");
    }

    @Override
    public void fileClosed(SrvSession srvSession, NetworkFile networkFile) {
        System.out.println("---------------------- [CIFS] File closed!");
    }

    @Override
    public void fileOpened(SrvSession srvSession, NetworkFile networkFile) {
        System.out.println("---------------------- [CIFS] File closed!");
    }
}
