// GetStatusOfRun returns status and result of run specified by ARN
func GetStatusOfRun(client *devicefarm.DeviceFarm, arn string) (string, string) {
	params := &devicefarm.GetRunInput{
		Arn: aws.String(arn),
	}
	resp, err := client.GetRun(params)
	errors.Validate(err, "Can't get status of run")
	return *resp.Run.Status, *resp.Run.Result
}
/* Get Run Status */
func runStatus(svc *devicefarm.DeviceFarm, runArn string) {

	infoReq := &devicefarm.GetRunInput{
		Arn: aws.String(runArn),
	}

	resp, err := svc.GetRun(infoReq)

	failOnErr(err, "error getting run info")
	fmt.Println(*resp.Run.Status)
}
/* Get Run Info */
func runInfo(svc *devicefarm.DeviceFarm, runArn string) {

	infoReq := &devicefarm.GetRunInput{
		Arn: aws.String(runArn),
	}

	resp, err := svc.GetRun(infoReq)

	failOnErr(err, "error getting run info")
	fmt.Println(awsutil.Prettify(resp))
}
/* Schedule Run */
func scheduleRun(svc *devicefarm.DeviceFarm, projectArn string, runName string, deviceArn string, devicePoolArn string, appArn string, appFile string, appType string, testPackageArn string, testPackageFile string, testType string) (scheduleError error) {

	debug := true
	// Upload the app file if there is one
	if appFile != "" {

		// Try to guess the upload type based on the filename
		if appType == "" {
			guessedType, err := guessAppType(appFile)
			appType = guessedType

			if err != nil {
				return err
			}
		}

		// Upload appFile with correct AppType
		fmt.Printf("- Uploading app-file %s of type %s ", appFile, appType)

		uploadApp, err := uploadPut(svc, appFile, appType, projectArn, "")
		if err != nil {
			fmt.Printf("Error: %s", err)
			return err
		}

		fmt.Printf("\n")
		appArn = *uploadApp.Arn
	}

	if devicePoolArn == "" {
		if deviceArn != "" {
			// Try to create pool from device Arn
			fmt.Printf("- Creating device pool %s ", deviceArn)
			foundArn, err := createPoolFromDevice(svc, deviceArn, deviceArn, projectArn)

			if err != nil {
				fmt.Printf("Error: %s", err)
				return err
			}
			devicePoolArn = foundArn
		} else {
			return errors.New("we need a device/devicepool to run on")
		}
	}

	// Try to guess the upload type based on the filename
	/*
		if testType == "" {
			testType, err := guessTestType(testPackageFile)
		}
	*/

	fmt.Printf("- Lookup test type %s \n", testType)
	testPackageType, err := lookupTestPackageType(testType)
	if err != nil {
		fmt.Printf("Error: %s", err)
		return err
	}

	// Upload the testPackage file if there is one
	if testPackageFile != "" {

		fmt.Printf("- Uploading test-file %s of type %s ", testPackageFile, testPackageType)

		uploadTestPackage, err := uploadPut(svc, testPackageFile, testPackageType, projectArn, "")

		if err != nil {
			return err
		}
		testPackageArn = *uploadTestPackage.Arn
		fmt.Printf("\n")
	}

	fmt.Printf("- Creating schedule run\n")
	param := make(map[string]*string)
	seed := "1234"
	throttle := "50"
	event_count := "1500"
	param["seed"] = &seed
	param["throttle"] = &throttle
	param["event_count"] = &event_count

	runTest := &devicefarm.ScheduleRunTest{
		Type: aws.String(testType),
		//TestPackageArn: aws.String(testPackageArn),
		Parameters: param, // test parameters
		//Filter: // filter to pass to tests
	}

	if debug {
		fmt.Println(appArn)
		fmt.Println(devicePoolArn)
		fmt.Println(runName)
		fmt.Println(testPackageArn)
		fmt.Println(testPackageType)
		fmt.Println(projectArn)
	}

	locale := "ja_JP"
	metered := devicefarm.BillingMethodMetered
	latitude := 35.676833
	longitude := 139.770139

	bool_true := true

	configuration := &devicefarm.ScheduleRunConfiguration{
		AuxiliaryApps: []*string{},
		BillingMethod: &metered,
		Locale:        &locale,
		Location: &devicefarm.Location{
			Latitude:  &latitude,
			Longitude: &longitude,
		},
		Radios: &devicefarm.Radios{
			Bluetooth: &bool_true,
			Gps:       &bool_true,
			Nfc:       &bool_true,
			Wifi:      &bool_true,
		},
	}

	runReq := &devicefarm.ScheduleRunInput{
		AppArn:        aws.String(appArn),
		Configuration: configuration,
		DevicePoolArn: aws.String(devicePoolArn),
		Name:          aws.String(runName),
		ProjectArn:    aws.String(projectArn),
		Test:          runTest,
	}

	if debug {
		fmt.Println(awsutil.Prettify(runReq))
	}

	fmt.Println("- Initiating test run")

	resp, err := svc.ScheduleRun(runReq)
	if err != nil {
		return err
	}

	//fmt.Println(awsutil.Prettify(resp))

	// Now we wait for the run status to go COMPLETED
	fmt.Print("- Waiting until the tests complete ")

	runArn := *resp.Run.Arn

	status := ""
	for status != "COMPLETED" {
		time.Sleep(4 * time.Second)
		infoReq := &devicefarm.GetRunInput{
			Arn: aws.String(runArn),
		}

		fmt.Print(".")
		resp, err := svc.GetRun(infoReq)

		if err != nil {
			return err
		}
		status = *resp.Run.Status
	}

	// Generate report
	fmt.Println("\n- Generating report ")
	runReport(svc, runArn)

	return nil

}
/* Get Run Report */
func runReport(svc *devicefarm.DeviceFarm, runArn string) {

	infoReq := &devicefarm.GetRunInput{
		Arn: aws.String(runArn),
	}

	resp, err := svc.GetRun(infoReq)

	failOnErr(err, "error getting run info")

	fmt.Printf("Reporting on run %s\n", *resp.Run.Name)
	fmt.Printf("Run arn %s\n", runArn)
	//fmt.Println(awsutil.Prettify(resp))

	jobReq := &devicefarm.ListJobsInput{
		Arn: aws.String(runArn),
	}

	// Find all artifacts
	artifactReq := &devicefarm.ListArtifactsInput{
		Arn: aws.String(runArn),
	}

	types := []string{"LOG", "SCREENSHOT", "FILE"}
	artifacts := map[string][]devicefarm.ListArtifactsOutput{}

	for _, artifactType := range types {

		artifactReq.Type = aws.String(artifactType)

		artifactResp, err := svc.ListArtifacts(artifactReq)
		failOnErr(err, "error getting run info")

		// Store type artifacts
		artifacts[artifactType] = append(artifacts[artifactType], *artifactResp)

	}

	respJob, err := svc.ListJobs(jobReq)
	failOnErr(err, "error getting jobs")

	// Find all jobs within this run
	for _, job := range respJob.Jobs {

		fmt.Println("==========================================")
		time.Sleep(2 * time.Second)

		jobFriendlyName := fmt.Sprintf("%s - %s - %s", *job.Name, *job.Device.Model, *job.Device.Os)

		fmt.Println(awsutil.Prettify(job))

		suiteReq := &devicefarm.ListSuitesInput{
			Arn: aws.String(*job.Arn),
		}
		suiteResp, err := svc.ListSuites(suiteReq)
		failOnErr(err, "error getting run info")

		for _, suite := range suiteResp.Suites {
			message := ""
			if suite.Message != nil {
				message = *suite.Message
			}

			debug := true
			if debug {
				fmt.Printf("%s -> %s : %s \n----> %s\n", jobFriendlyName, *suite.Name, message, *suite.Arn)
			}
			dirPrefix := fmt.Sprintf("report/%s/%s", jobFriendlyName, *suite.Name)
			downloadArtifactsForSuite(dirPrefix, artifacts, *suite)
		}

		fmt.Println(awsutil.Prettify(suiteResp))
	}

	params := &devicefarm.ListUniqueProblemsInput{
		Arn: aws.String(runArn), // Required
	}

	problems, err := svc.ListUniqueProblems(params)

	if err != nil {
		// Print the error, cast err to awserr.Error to get the Code and
		// Message from an error.
		fmt.Println(err.Error())
		return
	}

	// Pretty-print the response data.
	fmt.Println(awsutil.Prettify(problems))
	problemsJson, _ := jsonutil.BuildJSON(problems)
	writeFile(problemsJson, "./report/unique_problems.json")
}
/* Get Run Report */
func runReport(svc *devicefarm.DeviceFarm, runArn string) {

	infoReq := &devicefarm.GetRunInput{
		ARN: aws.String(runArn),
	}

	resp, err := svc.GetRun(infoReq)

	failOnErr(err, "error getting run info")
	fmt.Printf("%s\n", *resp.Run.Name)
	//fmt.Println(awsutil.Prettify(resp))

	jobReq := &devicefarm.ListJobsInput{
		ARN: aws.String(runArn),
	}

	// Find all artifacts
	artifactReq := &devicefarm.ListArtifactsInput{
		ARN: aws.String(runArn),
	}

	types := []string{"LOG", "SCREENSHOT", "FILE"}
	artifacts := map[string][]devicefarm.ListArtifactsOutput{}

	for _, artifactType := range types {

		artifactReq.Type = aws.String(artifactType)

		artifactResp, err := svc.ListArtifacts(artifactReq)
		failOnErr(err, "error getting run info")

		// Store type artifacts
		artifacts[artifactType] = append(artifacts[artifactType], *artifactResp)
		/*
			for _, artifact := range artifactResp.Artifacts {
				fmt.Println(awsutil.Prettify(artifact))
			}
		*/
	}

	respJob, err := svc.ListJobs(jobReq)
	failOnErr(err, "error getting jobs")

	for _, job := range respJob.Jobs {

		fmt.Println("==========================================")
		time.Sleep(2 * time.Second)

		jobFriendlyName := fmt.Sprintf("%s - %s - %s", *job.Name, *job.Device.Model, *job.Device.Os)

		//fmt.Println(awsutil.Prettify(job))

		suiteReq := &devicefarm.ListSuitesInput{
			ARN: aws.String(*job.ARN),
		}
		suiteResp, err := svc.ListSuites(suiteReq)
		failOnErr(err, "error getting run info")

		for _, suite := range suiteResp.Suites {
			message := ""
			if suite.Message != nil {
				message = *suite.Message
			}

			fmt.Printf("%s -> %s : %s \n----> %s\n", jobFriendlyName, *suite.Name, message, *suite.ARN)
			dirPrefix := fmt.Sprintf("report/%s/%s/", jobFriendlyName, *suite.Name)
			downloadArtifactsForSuite(dirPrefix, artifacts, *suite)
		}

		//fmt.Println(awsutil.Prettify(suiteResp))
	}

}
/* Schedule Run */
func scheduleRun(svc *devicefarm.DeviceFarm, projectArn string, runName string, deviceArn string, devicePoolArn string, appArn string, appFile string, appType string, testPackageArn string, testPackageFile string, testType string) (scheduleError error) {

	debug := false
	// Upload the app file if there is one
	if appFile != "" {

		// Try to guess the upload type based on the filename
		if appType == "" {
			guessedType, err := guessAppType(appFile)
			appType = guessedType

			if err != nil {
				return err
			}
		}

		// Upload appFile with correct AppType
		fmt.Printf("- Uploading app-file %s of type %s ", appFile, appType)

		uploadApp, err := uploadPut(svc, appFile, appType, projectArn, "")
		if err != nil {
			return err
		}

		fmt.Printf("\n")
		appArn = *uploadApp.ARN
	}

	if devicePoolArn == "" {
		if deviceArn != "" {
			// Try to create pool from device ARN
			foundArn, err := createPoolFromDevice(svc, deviceArn, deviceArn, projectArn)

			if err != nil {
				return err
			}
			devicePoolArn = foundArn
		} else {
			return errors.New("we need a device/devicepool to run on")
		}
	}

	// Try to guess the upload type based on the filename
	/*
		if testType == "" {
			testType, err := guessTestType(testPackageFile)
		}
	*/

	testPackageType, err := lookupTestPackageType(testType)
	if err != nil {
		return err
	}

	// Upload the testPackage file if there is one
	if testPackageFile != "" {

		fmt.Printf("- Uploading test-file %s of type %s ", testPackageFile, testPackageType)

		uploadTestPackage, err := uploadPut(svc, testPackageFile, testPackageType, projectArn, "")
		if err != nil {
			return err
		}
		testPackageArn = *uploadTestPackage.ARN
		fmt.Printf("\n")
	}

	runTest := &devicefarm.ScheduleRunTest{
		Type:           aws.String(testType),
		TestPackageARN: aws.String(testPackageArn),
		//Parameters: // test parameters
		//Filter: // filter to pass to tests
	}

	if debug {
		fmt.Println(appArn)
		fmt.Println(devicePoolArn)
		fmt.Println(runName)
		fmt.Println(testPackageArn)
		fmt.Println(testPackageType)
		fmt.Println(projectArn)
	}

	runReq := &devicefarm.ScheduleRunInput{
		AppARN:        aws.String(appArn),
		DevicePoolARN: aws.String(devicePoolArn),
		Name:          aws.String(runName),
		ProjectARN:    aws.String(projectArn),
		Test:          runTest,
	}

	if debug {
		fmt.Println(awsutil.Prettify(runReq))
	}

	fmt.Println("- Initiating test run")

	resp, err := svc.ScheduleRun(runReq)
	if err != nil {
		return err
	}

	//fmt.Println(awsutil.Prettify(resp))

	// Now we wait for the run status to go COMPLETED
	fmt.Print("- Waiting until the tests complete ")

	runArn := *resp.Run.ARN

	status := ""
	for status != "COMPLETED" {
		time.Sleep(4 * time.Second)
		infoReq := &devicefarm.GetRunInput{
			ARN: aws.String(runArn),
		}

		fmt.Print(".")
		resp, err := svc.GetRun(infoReq)

		if err != nil {
			return err
		}
		status = *resp.Run.Status
	}

	// Generate report
	fmt.Println("\n- Generating report ")
	runReport(svc, runArn)

	return nil

}