/*  Account
 *
 *  Copyright (C) 2023  Rene Pawlitzek, Robert Schoech
 *  
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

import java.net.URL;
import twofactor.Utils;
import java.io.InputStream;
import java.io.File;

public class Account extends AccountAPI {
    
    private final String codeFileName = "QRCode.png";
    
    public String createSecretKey() {
        return Utils.generateSecretKey();
    }
    
    public String createQRCode(String secretKey, String name, String company) throws Exception {
        var barCodeUrl = Utils.getGoogleAuthenticatorBarCode(secretKey, name, company);
        String fileName = ".\\data\\" + codeFileName;
        Utils.createQRCode(barCodeUrl, fileName, 400, 400);
        return fileName;
    }
    
    public void deleteQRCode() {
        String fileName = ".\\data\\" + codeFileName;
        File file = new File(fileName);
        if (file.exists()) {
            file.delete();
        }
   }
    
    public boolean verifyTOTPCode(String name, String code) throws Exception {
        var secretKey = getSecretKey(name);
        if (code.equals(Utils.getTOTPCode(secretKey))) {
            return true;
        } 
        return false;            
    }
    
}
