package app.controller;



import app.model.Users;

import ca.tecreations.TextFile;
import ca.tecreations.TypeToType;

import jakarta.servlet.http.HttpSession;

import java.util.Collection;
import java.util.Iterator;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.csrf.CsrfToken;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; // "F:\projects\security1\jars\spring-web-6.1.8.jar"
import org.springframework.web.bind.annotation.RequestMethod; // "F:\projects\security1\jars\spring-web-6.1.8.jar"
import org.springframework.web.bind.annotation.ResponseBody; // "F:\projects\security1\jars\spring-web-6.1.8.jar"
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.RedirectView;

@RestController
public class WelcomeController {
    private final String HOME_VIEW_COUNT = "HOME_VIEW_COUNT";
     
    @GetMapping("/count")
    public String getCount(HttpSession session) {
        return "HOME_VIEW_COUNT: " + session.getAttribute(HOME_VIEW_COUNT);
    }
    
    public void incrementCount(HttpSession session,String attr) {
        Object val = session.getAttribute(attr);
        var homeViewCount = val == null ? 0 : (Integer) val;
        session.setAttribute(attr,homeViewCount + 1);
    }
    
    @GetMapping("/account/admin")
    public String welcomeAdmin(Authentication authentication,HttpSession session,CsrfToken csrfToken) {
        return getString(authentication,session,csrfToken);
    }
     
    @GetMapping("/account/sysop")
    public String welcomeSysop(Authentication authentication,HttpSession session,CsrfToken csrfToken) {
        return getString(authentication,session,csrfToken);
    }
     
    @GetMapping("/account/user")
    public String welcomeUser(Authentication authentication,HttpSession session,CsrfToken csrfToken) {
        return getString(authentication,session,csrfToken);
    }
     
    @PostMapping("/account/user_type_redirect")
    public RedirectView userTypeRedirect(Authentication authentication, HttpSession session) {
        String userType = getUserType(authentication,session);
        if (userType.equals("SYSOP")) {
            return new RedirectView("/account/sysop");
        } else if (userType.equals("ADMIN")) {
            return new RedirectView("/account/admin");
        } else {
            return new RedirectView("/account/user");
        }
    }
    
    // redirect example from google
    //@GetMapping("/old-path")
    //public String handleOldPath() {
        // Perform some logic
    //    return "redirect:/new-path"; // Redirects to the /new-path URL
    //}   
    
    public String getUserType(Authentication authentication, HttpSession session) {
        String s = "";
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        Iterator<? extends GrantedAuthority> it = authorities.iterator();
        while (it.hasNext()) {
            String next = it.next().getAuthority();
            if (next.equals("ROLE_SYSOP")) return "SYSOP";
            else if (next.equals("ROLE_ADMIN")) return "ADMIN";
            else if (next.equals("ROLE_USER")) return "USER";
            else {
                System.out.println("WelcomeController.getUserType: found: " + next);
            }
        }
        return "WelcomeController.getUserType: unknown user type";
    }
    
    public String getString(Authentication authentication, HttpSession session,CsrfToken csrfToken) {
        String s = "";
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        if (authorities.contains("SYSOP")) {
            s += "SYSOP<br />\n";
        } else if (authorities.contains("ADMIN")) {
            s += "ADMIN<br />\n";
        } else if (authorities.contains("USER")) {
            s += "USER<br />\n";
        }
        String principal;
        if (authentication.getPrincipal() instanceof Users) {
            principal = ((Users)authentication.getPrincipal()).getEmail();
        } else {
            principal = (String)authentication.getPrincipal();
        }

        String sessionStr = session.toString();
        incrementCount(session,HOME_VIEW_COUNT);
        s += "<h1>Welcome, " + principal + " -- " + getCount(session) + "</h1>\n" +
               "<h4>Authentication:</h4>\n" +
               "<p>" + authentication.toString() + "</p>\n" +
               "<h4>Session</h4>\n" + 
               "<p>" + sessionStr + "</p>" +
               "<p>" + session.getId() + "</p>" +
        //       "<p><a href='/logout'>Logout</a></p>";
                "<form action='/logout' method='POST'>\n" +
                "  <input type='hidden' name='_csrf' value='" + csrfToken.getToken() + "' />\n" + 
                "  <input type='submit' value='Logout' />\n" + 
                "</form>\n" +
                "<form action='/account/change_pass' method='GET'>\n" +
                "  <input type='hidden' name='_csrf' value='" + csrfToken.getToken() + "' />\n" + 
                "  <input type='submit' value='Change Pass' />\n" + 
                "</form>\n"
        ;
        return s;
    }
}
