package CHAPTER04;

import java.util.Scanner;

public class ZREAL_Sample_EmailAnalyzer {
	
	public static void main(String[] args) {
		
        Scanner sc = new Scanner(System.in);

        // 1) 입력 및 전처리
        System.out.print("이름 입력: ");
        String rawName = sc.nextLine().trim();

        System.out.print("이메일 입력: ");
        String rawEmail = sc.nextLine().trim();

        // 2) 이름 검증 및 정규화
        if (rawName.isEmpty()) {
            System.out.println("이름을 올바르게 입력해 주세요.");
            sc.close();
            return;
        }
        String convertedName;
        if (rawName.length() == 1) {
            convertedName = rawName.toUpperCase();
        } else {
            convertedName = rawName.substring(0, 1).toUpperCase()
                    + rawName.substring(1).toLowerCase();
        }

        // 3) 이메일 형식 검증
        int atFirst = rawEmail.indexOf('@');
        int atLast = rawEmail.lastIndexOf('@');

        if (rawEmail.isEmpty()) {
            System.out.println("이메일을 입력해 주세요.");
            sc.close();
            return;
        }
        if (atFirst == -1) {
            System.out.println("유효하지 않은 이메일 형식입니다: '@'가 없습니다.");
            sc.close();
            return;
        }
        if (atFirst != atLast) {
            System.out.println("유효하지 않은 이메일 형식입니다: '@'가 두 개 이상입니다.");
            sc.close();
            return;
        }
        // '@' 앞/뒤 비어있는지 확인
        if (atFirst == 0 || atFirst == rawEmail.length() - 1) {
            System.out.println("유효하지 않은 이메일 형식입니다: 아이디 또는 도메인이 비어 있습니다.");
            sc.close();
            return;
        }

        // 4) 아이디/도메인 분해
        String id = rawEmail.substring(0, atFirst);
        String domain = rawEmail.substring(atFirst + 1);
        String domainLower = domain.toLowerCase();

        if (id.isEmpty() || domain.isEmpty()) {
            System.out.println("유효하지 않은 이메일 형식입니다: 아이디 또는 도메인이 비어 있습니다.");
            sc.close();
            return;
        }

        // 5) TLD 추출 및 switch 분류
        String tld = "";
        int lastDot = domainLower.lastIndexOf('.');
        if (lastDot != -1 && lastDot < domainLower.length() - 1) {
            tld = domainLower.substring(lastDot + 1);
        } else {
            tld = "(없음)";
        }

        String tldCategory;
        switch (tld) {
            case "com":
                tldCategory = "일반 상용(com)";
                break;
            case "net":
                tldCategory = "네트워크(net)";
                break;
            case "org":
                tldCategory = "비영리(org)";
                break;
            case "edu":
                tldCategory = "교육(edu)";
                break;
            case "kr":
                tldCategory = "국가(kr)";
                break;
            default:
                tldCategory = "기타";
        }

        // 6) 서비스 제공자 판별 (if-else)
        String provider;
        if (domainLower.equals("gmail.com")) {
            provider = "구글(Gmail)";
        } else if (domainLower.equals("naver.com")) {
            provider = "네이버 메일";
        } else if (domainLower.equals("daum.net")) {
            provider = "다음 메일";
        } else if (domainLower.equals("outlook.com")) {
            provider = "마이크로소프트(Outlook)";
        } else if (domainLower.equals("kakao.com")) {
            provider = "카카오 메일";
        } else {
            provider = "기타";
        }

        // 7) 출력
        System.out.println("변환된 이름: " + convertedName);
        System.out.println("이메일 아이디: " + id);
        System.out.println("이메일 도메인: " + domain);
        System.out.println("도메인 TLD 분류: " + tldCategory);
        System.out.println("서비스 제공자 판별: " + provider);
        System.out.println("검증 메시지: 유효한 이메일 형식입니다.");

        sc.close();
    }
}
