import os
import cv2
import numpy as np
import imagehash
from PIL import Image
import ffmpeg
from flask import Flask, request, jsonify

app = Flask(__name__)

# Function to resize images for different platforms
def resize_image(input_path, output_path, size):
    img = Image.open(input_path)
    img = img.resize(size, Image.ANTIALIAS)
    img.save(output_path)
    return output_path

# Face Swapping (Basic OpenCV Implementation)
def face_swap(source_path, target_path, output_path):
    source_img = cv2.imread(source_path)
    target_img = cv2.imread(target_path)
    
    # Convert to grayscale
    source_gray = cv2.cvtColor(source_img, cv2.COLOR_BGR2GRAY)
    target_gray = cv2.cvtColor(target_img, cv2.COLOR_BGR2GRAY)

    # Load OpenCV face detector
    detector = cv2.CascadeClassifier(cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
    
    source_faces = detector.detectMultiScale(source_gray, 1.1, 4)
    target_faces = detector.detectMultiScale(target_gray, 1.1, 4)

    if len(source_faces) == 0 or len(target_faces) == 0:
        return "No face detected!"

    for (x, y, w, h) in target_faces:
        face = cv2.resize(source_img, (w, h))
        target_img[y:y+h, x:x+w] = face

    cv2.imwrite(output_path, target_img)
    return output_path

# Detect Similarity Between Files
def detect_similarity(file1, file2):
    img1 = Image.open(file1)
    img2 = Image.open(file2)

    hash1 = imagehash.phash(img1)
    hash2 = imagehash.phash(img2)

    similarity = 1 - (hash1 - hash2) / len(hash1.hash) ** 2
    return similarity

# TikTok Spoofer (Modify Metadata)
def spoof_tiktok_metadata(input_video, output_video):
    ffmpeg.input(input_video).output(output_video, metadata="title=New TikTok Video").run()
    return output_video

# Flask API Routes
@app.route('/resize', methods=['POST'])
def api_resize():
    data = request.json
    input_path = data['input_path']
    output_path = data['output_path']
    size = tuple(data['size'])
    
    result = resize_image(input_path, output_path, size)
    return jsonify({"message": "Image resized!", "output": result})

@app.route('/face_swap', methods=['POST'])
def api_face_swap():
    data = request.json
    source_path = data['source']
    target_path = data['target']
    output_path = data['output']
    
    result = face_swap(source_path, target_path, output_path)
    return jsonify({"message": "Face swap complete!", "output": result})

@app.route('/detect_similarity', methods=['POST'])
def api_detect_similarity():
    data = request.json
    file1 = data['file1']
    file2 = data['file2']
    
    similarity = detect_similarity(file1, file2)
    return jsonify({"similarity_score": similarity})

@app.route('/spoof_tiktok', methods=['POST'])
def api_spoof_tiktok():
    data = request.json
    input_video = data['input_video']
    output_video = data['output_video']
    
    result = spoof_tiktok_metadata(input_video, output_video)
    return jsonify({"message": "TikTok metadata spoofed!", "output": result})

if __name__ == '__main__':
    app.run(debug=True)