package Ming;

import org.apache.commons.cli.*;

import java.io.File;
import java.io.IOException;

public class Main {
    public static void main(String[] args) {
        if (args.length == 0) { // 임시 코드
            SpeechToTextService service = new SpeechToTextService();

            System.out.println("Trying to recognize ... ");
            String result = service.recognize(new File("src/test/resources/sample_audio/open-the-goddamn-door.wav"));
            System.out.println("Result: " + result);
            return;
        }
        Options options = new Options();
        options.addOption(Option.builder("h")
                .longOpt("help")
                .desc("print this message")
                .build()
        );
        options.addOption(Option.builder()
                .longOpt("test")
                .desc("trigger a test method for development experience.")
                .hasArg()
                .argName("testMethodName")
                .build()
        );

        try {
            CommandLineParser parser = new DefaultParser();
            CommandLine cmd = parser.parse(options, args);
            if (cmd.hasOption("help")) {
                // automatically generate the help statement
                HelpFormatter formatter = new HelpFormatter();
                formatter.printHelp("ming", options, true);
                return;
            }
            if (cmd.hasOption("test")) {
                String[] targets = cmd.getArgs();
                switch (cmd.getOptionValue("test")) {
                    case "extract_sound": {
                        if (targets.length < 2) {
                            System.out.println("you must provide [src_video_file] [dst_audio_file_path]");
                            return;
                        }
                        String source = targets[0];
                        String destinationPath = targets[1];
                        try {
                            AudioExtractor extractor = new AudioExtractor(source);
                            long offset = 33 * 60 * 1000 + 676;
                            long duration = 21 * 1000 + 133;
                            File output = new File(extractor.extract(offset, duration));
                            File dest = new File(destinationPath);
                            output.renameTo(dest);
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    break;
                    case "read_srt":
                        if (targets.length < 1) {
                            System.out.println("you must provide [srt_file]");
                            break;
                        }
                        SRTReader srtReader = new SRTReader();
                        try {
                            System.out.println(targets[0]);
                            Subtitle st = srtReader.read(new File(targets[0]));
                            System.out.println(st.items.size());
                            Subtitle.Item item = st.items.get(2411);
                            System.out.println(item.startTimestamp);
                            System.out.println(item.endTimestamp);
                            System.out.println("_" + item.text + "_");

                        } catch (java.text.ParseException e) {
                            e.printStackTrace();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                        break;
                    case "senario1":
                        if (targets.length < 3) {
                            System.out.println("you must provide [video_file] [srt_file] [current_playback_position]");
                            break;
                        }
                        String videoFilepath = targets[0];
                        String srtFilepath = targets[1];
                        long playback_position = Integer.parseInt(targets[2]);
                        // TODO:
                        // 1. Find subtitle items by current playback
                        // 2. Extract audio
                        // 3. Send the audio to the speech to text service.
                        // 4. Compare the text and subtitle contents.
                        break;
                    default:
                        throw new UnsupportedOperationException("commons-cli 에 테스트 시나리오가 연결 되지 않음!");
                }
                return;
            }
            throw new UnsupportedOperationException("commons-cli 에 처리 되지 못 한 사용 예.");
        } catch (ParseException e) {
            e.printStackTrace();
        }
    }
}
@@ -4083,16 +4083,13 @@
     
-// TODO:
+try %7B
 %0A   
@@ -4113,243 +4113,1619 @@
     
-// 1. Find subtitle items by current playback%0A                        // 2. Extract audio%0A                        // 3. Send the audio to the speech to text service.%0A                        // 4. Compare the text and subtitle contents.
+    System.out.println(%22Find subtitle%22);%0A                            Subtitle subs = new SRTReader().read(new File(srtFilepath));%0A                            int position = -1;%0A                            for (int i=0; i%3Csubs.items.size(); i++) %7B%0A                                if (subs.items.get(i).startTimestamp %3E playback_position) %7B%0A                                    position = i;%0A                                    break;%0A                                %7D%0A                            %7D%0A                            if (position %3C 0) %7B%0A                                System.out.println(%22Unavailable playback position: %22 + playback_position);%0A                                return;%0A                            %7D%0A                            System.out.println(%22Extract Audio%22);%0A                            AudioExtractor extractor_ = new AudioExtractor(videoFilepath);%0A                            File output = new File(extractor_.extract(subs.items.get(position).startTimestamp, subs.items.get(position).endTimestamp - subs.items.get(position).startTimestamp));%0A                            System.out.println(%22Speech To Text: %22 + output.getAbsolutePath());%0A                            SpeechToTextService service = new SpeechToTextService();%0A                            String result = service.recognize(output);%0A                            System.out.println(result);%0A                            // TODO:%0A                            // 4. Compare the text and subtitle contents.%0A                        %7D catch (Exception e) %7B%0A                            e.printStackTrace();%0A                        %7D
 %0A   

Implement detecting algorithm senario 1.