package de.oaa.xxx.games.chastity.ttlock;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import de.oaa.xxx.games.chastity.ttlock.TTLockService.TTLockDetailResponse;

@RestController
@RequestMapping("/ttlock")
public class TTLockTest {

	private final TTAuthService auth;
	private final TTLockService lock;
	
	private String clientId = "6e5077a84b6a4e1ba0fb6a8da21c6417";
	private String clientSecret = "a2c1d68c7905d52584fc29028937db11";
	private String username= "mario.stoermer@proton.me";
	private String password = "knall666.Halla";
	private int lockId = 30158446;
	
	public TTLockTest(TTAuthService auth, TTLockService lock) {
		this.auth = auth;
		this.lock = lock;
	}
	
	@GetMapping("/details")
	public ResponseEntity<TTLockDetailResponse> details() {
		String md5Hex = org.apache.commons.codec.digest.DigestUtils.md5Hex(password).toLowerCase();
		String token = auth.getAccessToken(clientId, clientSecret, username, md5Hex);
		return ResponseEntity.ok(lock.getLockDetail(clientId, token, lockId));
	}
	
	@GetMapping("/add/{pin}")
	public ResponseEntity<Integer> add(@PathVariable String pin) {
		String md5Hex = org.apache.commons.codec.digest.DigestUtils.md5Hex(password).toLowerCase();
		String token = auth.getAccessToken(clientId, clientSecret, username, md5Hex);
		return ResponseEntity.ok(lock.addCustomPasscode(clientId, token, lockId, pin));
	}
	
	@GetMapping("/delete/{id}")
	public ResponseEntity<String> remove(@PathVariable Integer id) {
		String md5Hex = org.apache.commons.codec.digest.DigestUtils.md5Hex(password).toLowerCase();
		String token = auth.getAccessToken(clientId, clientSecret, username, md5Hex);
		return ResponseEntity.ok(lock.deleteCustomPasscode(clientId, token, lockId, id));
	}
	
	@GetMapping("/delete/all")
	public void removeAll() {
		String md5Hex = org.apache.commons.codec.digest.DigestUtils.md5Hex(password).toLowerCase();
		String token = auth.getAccessToken(clientId, clientSecret, username, md5Hex);
		lock.findAndDeleteLocksByName(clientId, token, lockId);
	}
}
