/* Schedule Run */
func scheduleRun(svc *devicefarm.DeviceFarm, runName string, projectArn string, appUploadArn string, devicePoolArn string, testUploadArn string, testType string) {

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

	if testUploadArn != "" {
		runTest.TestPackageARN = aws.String(testUploadArn)
	}

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

	resp, err := svc.ScheduleRun(runReq)

	failOnErr(err, "error scheduling run")
	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

}
/* 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

}