// TestGermanDecryption tests the German decryption.
// Assumes that the input message is deserialized
// Decrypted string should match ÜÖ
func TestGermanDecryption(t *testing.T) {
	message := "stpgsG1DZZxb44J7mFNSzg=="
	//decrypt
	decrypted, decErr := pubnubMessaging.DecryptString("enigma", message)
	if decErr != nil {
		t.Error("German decryption: failed.")
	} else {
		data, _, _, err := pubnubMessaging.ParseJson([]byte(decrypted), "")
		if err != nil {
			t.Error("German decryption: failed.", err)
		} else {
			if "ÜÖ" == data {
				fmt.Println("German decryption: passed.")
			} else {
				t.Error("German decryption: failed.")
			}
		}
	}
}
// TestUnicodeDecryption tests the Unicode decryption.
// Assumes that the input message is deserialized
// Decrypted string should match 漢語
func TestUnicodeDecryption(t *testing.T) {
	message := "+BY5/miAA8aeuhVl4d13Kg=="
	//decrypt
	decrypted, decErr := pubnubMessaging.DecryptString("enigma", message)
	if decErr != nil {
		t.Error("Unicode decryption: failed.")
	} else {
		data, _, _, err := pubnubMessaging.ParseJson([]byte(decrypted), "")
		if err != nil {
			t.Error("Unicode decryption: failed.", err)
		} else {
			if "漢語" == data {
				fmt.Println("Unicode decryption: passed.")
			} else {
				t.Error("Unicode decryption: failed.")
			}
		}
	}
}
// ParseSubscribeData is used by multiple test cases and acts according to the testcase names.
// In case of complex message calls a sub method and in case of a simle message parses
// the response.
func ParseSubscribeData(t *testing.T, response []byte, testName string, cipherKey string) {
	if response != nil {
		var b interface{}
		err := json.Unmarshal(response, &b)

		isValid := false
		if (testName == "SubscriptionConnectedForComplex") || (testName == "SubscriptionConnectedForComplexWithCipher") {
			isValid = CheckComplexData(b)
		} else if (testName == "SubscriptionConnectedForSimple") || (testName == "SubscriptionConnectedForSimpleWithCipher") {
			data, _, _, err2 := pubnubMessaging.ParseJson(response, cipherKey)
			if err2 != nil {
				fmt.Println("err2:", err2)
			} else {
				var arr []string
				err3 := json.Unmarshal([]byte(data), &arr)
				if err3 != nil {
					fmt.Println("err3:", err2)
				} else {
					if len(arr) > 0 {
						if arr[0] == "Test message" {
							isValid = true
						}
					}
				}
			}
		}
		if err != nil {
			fmt.Println("err2:", err)
			fmt.Println("Test '" + testName + "': failed.")
			t.Error("Test '" + testName + "': failed.")
		} else if !isValid {
			fmt.Println("Test '" + testName + "': failed.")
			t.Error("Test '" + testName + "': failed.")
		} else {
			fmt.Println("Test '" + testName + "': passed.")
		}
	}
}
// The method that parses the presence response sets the global
// variable _endPresenceTestAsSuccess to true if the presence contains a join info
// on the channel and _endPresenceTestAsFailure is otherwise.
func ParsePresenceResponse(pubnubInstance *pubnubMessaging.Pubnub, t *testing.T, returnChannel chan []byte, channel string, testName string, customUuid string, testConnected bool) bool {
	for {
		value, ok := <-returnChannel
		if !ok {
			break
		}
		if string(value) != "[]" {
			response := fmt.Sprintf("%s", value)
			//fmt.Println("resp pres:", response)
			messagePresence := "Presence notifications for channel '" + channel + "' connected"
			messagePresenceReconn := "Presence notifications for channel '" + channel + "' reconnected"

			if testConnected && ((strings.Contains(response, messagePresence)) || (strings.Contains(response, messagePresenceReconn))) {
				return true
			} else if !testConnected {

				message := "'" + channel + "' disconnected"
				messageConn := "'" + channel + "' connected"
				messageReconn := "'" + channel + "' reconnected"
				if !strings.Contains(response, message) && (!strings.Contains(response, messageConn)) && (!strings.Contains(response, messageReconn)) {
					data, _, returnedChannel, err2 := pubnubMessaging.ParseJson(value, "")
					var occupants []struct {
						Action    string
						Uuid      string
						Timestamp float64
						Occupancy int
					}
					//fmt.Println("data '" + testName + "':",data)
					if err2 != nil {
						fmt.Println("err2 '"+testName+"':", err2)
						_endPresenceTestAsFailure = true
						break
					}

					err := json.Unmarshal([]byte(data), &occupants)
					if err != nil {
						fmt.Println("err '"+testName+"':", err)
						_endPresenceTestAsFailure = true
						break
					} else {
						channelSubRepsonseReceived := false
						for i := 0; i < len(occupants); i++ {
							if (occupants[i].Action == "join") && occupants[i].Uuid == customUuid {
								channelSubRepsonseReceived = true
								break
							}
						}
						if !channelSubRepsonseReceived {
							fmt.Println("Test '" + testName + "': failed. Err2")
							_endPresenceTestAsFailure = true
							break
						}
						if channel == returnedChannel {
							_endPresenceTestAsSuccess = true
							return true
						} else {
							fmt.Println("Test '" + testName + "': failed. Err3")
							_endPresenceTestAsFailure = true
							break
						}
					}
				}
			}
		}
	}
	return false
}