package com.hclsoftware.sso.kerberos.controller;

import com.hclsoftware.sso.kerberos.service.IBMiConnectorService;
import com.hclsoftware.sso.kerberos.utils.Serializer;
import com.ibm.as400.access.*;
import com.ibm.as400.security.auth.ProfileTokenCredential;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.ietf.jgss.GSSCredential;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.GSSManager;
import org.ietf.jgss.Oid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.http.HttpStatusCode;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.kerberos.authentication.JaasSubjectHolder;
import org.springframework.security.kerberos.authentication.KerberosUsernamePasswordAuthenticationToken;
import org.springframework.security.kerberos.authentication.sun.SunJaasKerberosTicketValidator;
import org.springframework.util.SerializationUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.security.auth.Subject;
import javax.security.auth.kerberos.KerberosTicket;
import javax.security.auth.login.LoginException;
import java.beans.PropertyVetoException;
import java.io.IOException;
import java.security.PrivilegedAction;
import java.security.PrivilegedExceptionAction;
import java.util.Optional;
import java.util.Set;

@Slf4j
@RestController
@RequestMapping("/endpoint")
public class SampleController {


    @Value("${app.as400Host}")
    private String as400Host;

    @Autowired
    private IBMiConnectorService ibMiConnectorService;
    @SneakyThrows
    @GetMapping
    public ResponseEntity getIt() {
        //GSSCredential gssCredential =ibmIMc();
        ProfileTokenCredential gssCredential1 =ibMiConnectorService.ibmIMc();
        gssCredential1.setTokenType(ProfileTokenCredential.TYPE_MULTIPLE_USE_RENEWABLE);
        //gssCredential.setTimeoutInterval(3600*24);

        AS400 as400_1 = new AS400(as400Host, gssCredential1);

        //gssCredential1.
        //
        String fullProgramName1 = "/QSYS.lib/DOUGLAS.lib/DUMMY01.pgm";
        ProgramParameter[] parameterList = new ProgramParameter[1];
        parameterList[0] = new ProgramParameter(111);
//			parameterList[1] = new ProgramParameter(123);

        ProgramCall programCall1 = new ProgramCall(as400_1);
//			programCall.setProgram(fullProgramName);//, parmList);
        programCall1.setProgram(fullProgramName1,parameterList);//, parmList);

        // Run the program.
        if (programCall1.run()) {
            System.out.println("----------------------------------------Success full------------------------------------------");
        }
        //

        byte[] serialize = Serializer.serialize(gssCredential1);//SerializationUtils.serialize(gssCredential);
        return new ResponseEntity<>(gssCredential1, HttpStatus.OK);
    }


}
