package main

import (
	"fmt"
	"github.com/Schmenn/discord-exploits/exploits"
	"github.com/Schmenn/discord-exploits/modules"
	"os"
	"strings"
)

var (
	quiet     bool   = false
	inputFile string = "no input file provided"
	mode      string = "no mode specified"
)

func main() {

	args := os.Args[1:]
	handleArgs(args, &quiet)
	if !quiet {
		modules.Welcome()
	}

	fmt.Println("input file: " + inputFile)
	fmt.Println("mode: " + mode)

	initCommand(inputFile, mode)

}

func handleArgs(args []string, quiet *bool) {

	for i, s := range args {
		switch s {
		// Quiet
		case "-q", "--quiet":
			*quiet = true

		// Input File
		case "-i":
			inputFile = args[i+1]

		// Mode Selection
		case "-m":
			mode = args[i+1]

		// Help Message
		case "-h":
			modules.Help(os.Args[0])
			*quiet = true
			return

		default:
			break
		}
	}

}

func initCommand(inputFile string, mode string) {
	inputFile = strings.ToLower(inputFile)

	if strings.HasSuffix(inputFile, ".webm") {

		switch mode {
		case "e":
			fmt.Println("editing video.")
			exploits.RunExpandingVideoTask(inputFile)
			fmt.Println("completed task.")

		case "n":
			fmt.Println("editing video.")
			exploits.RunNegativeVideoTask(inputFile)
			fmt.Println("completed task.")

		case "0", "z":
			fmt.Println("editing video.")
			exploits.RunZeroVideoTask(inputFile)
			fmt.Println("completed task.")

		case "c":
			fmt.Println("transcoding video from webm to mp4")
			out := modules.Transcode(inputFile, "mp4")
			fmt.Println("finished transcoding video from webm to mp4")
			fmt.Println("editing video.")
			exploits.RunCrashVideoTask(out)
			fmt.Println("completed task.")
			os.Remove(out)

		default:
			fmt.Println("the mode doesn't match the file")
		}

	} else if strings.HasSuffix(inputFile, ".mp4") {

		switch mode {
		case "e":
			fmt.Println("transcoding video from mp4 to webm")
			out := modules.Transcode(inputFile, "webm")
			fmt.Println("finished transcoding video from mp4 to webm")
			fmt.Println("editing video.")
			exploits.RunExpandingVideoTask(out)
			fmt.Println("completed task.")
			os.Remove(out)

		case "n":
			fmt.Println("transcoding video from mp4 to webm")
			out := modules.Transcode(inputFile, "webm")
			fmt.Println("finished transcoding video from mp4 to webm")
			fmt.Println("editing video.")
			exploits.RunNegativeVideoTask(out)
			fmt.Println("completed task.")
			os.Remove(out)

		case "0", "z":
			fmt.Println("transcoding video from mp4 to webm")
			out := modules.Transcode(inputFile, "webm")
			fmt.Println("finished transcoding video from mp4 to webm")
			fmt.Println("editing video.")
			exploits.RunZeroVideoTask(out)
			fmt.Println("completed task.")
			os.Remove(out)

		case "c":
			fmt.Println("editing video.")
			exploits.RunCrashVideoTask(inputFile)
			fmt.Println("completed task.")

		default:
			fmt.Println("the mode doesn't match the file")
		}

	} else if strings.HasSuffix(inputFile, ".png") {
		fmt.Println("editing photo.")
		exploits.RunVirusImageTask(inputFile)
		fmt.Println("completed task.")

	} else if strings.HasSuffix(inputFile, ".jpg") {
		fmt.Println("transcoding image from jpg to png")
		out := modules.Transcode(inputFile, "png")
		fmt.Println("finished transcoding image from jpg to png")
		fmt.Println("editing photo.")
		exploits.RunVirusImageTask(out)
		fmt.Println("completed task.")
		os.Remove(out)

	} else if strings.HasSuffix(inputFile, ".jpeg") {
		if strings.ToLower(mode) != "v" || strings.ToLower(mode) == "no mode specified" {
			fmt.Println("the mode is not compatible with the image, proceeding to run the virus image task anyway")
		}
		fmt.Println("transcoding image from jpeg to png")
		out := modules.Transcode(inputFile, "png")
		fmt.Println("finished transcoding image from jpeg to png")
		fmt.Println("editing photo.")
		exploits.RunVirusImageTask(out)
		fmt.Println("completed task.")
		os.Remove(out)

	} else if strings.HasSuffix(inputFile, ".ogg") {
		if strings.ToLower(mode) != "t" || strings.ToLower(mode) == "no mode specified" {
			fmt.Println("the mode is not compatible with the audio file, proceeding to run the play-twice task anyway")
		}
		fmt.Println("editing audio.")
		exploits.RunTwiceAudioTask(inputFile)
		fmt.Println("completed task.")

	} else if strings.HasSuffix(inputFile, ".mp3") {
		if strings.ToLower(mode) != "t" || strings.ToLower(mode) == "no mode specified" {
			fmt.Println("the mode is not compatible with the audio file, proceeding to run the play-twice task anyway")
		}
		fmt.Println("transcoding audio from mp3 to ogg")
		out := modules.Transcode(inputFile, "ogg")
		fmt.Println("finished transcoding audio from mp3 to ogg")
		fmt.Println("editing audio.")
		exploits.RunTwiceAudioTask(out)
		fmt.Println("completed task.")
		os.Remove(out)

	} else if strings.HasSuffix(inputFile, ".m4a") {
		if strings.ToLower(mode) != "t" || strings.ToLower(mode) == "no mode specified" {
			fmt.Println("the mode is not compatible with the audio file, proceeding to run the play-twice task anyway")
		}
		fmt.Println("transcoding audio from m4a to ogg")
		out := modules.Transcode(inputFile, "ogg")
		fmt.Println("finished transcoding audio from m4a to ogg")
		fmt.Println("editing audio.")
		exploits.RunTwiceAudioTask(out)
		fmt.Println("completed task.")
		os.Remove(out)

	} else if inputFile == "no input file provided" {
		return

	} else {
        fmt.Println("incompatible input file")

    }
}