//
//  MessagesViewController.swift
//  GIFCollector MessagesExtension
//
//  Created by Joshua Higgins on 6/2/25.
//

import UIKit
import Messages

class MessagesViewController: MSMessagesAppViewController {
    
    private var gifCollectionVC: GIFCollectionViewController?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupChildViewController()
    }
    
    private func setupChildViewController() {
        let collectionVC = GIFCollectionViewController()
        collectionVC.onSelectGIF = { [weak self] gif in
            self?.sendGIF(gif)
        }
        
        addChild(collectionVC)
        collectionVC.view.frame = view.bounds
        collectionVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        view.addSubview(collectionVC.view)
        collectionVC.didMove(toParent: self)
        
        gifCollectionVC = collectionVC
    }
    
    private func sendGIF(_ gif: GIF) {
        guard let conversation = activeConversation,
              let gifURL = gif.url else { return }
        
        // Show a loading indicator
        let loadingAlert = UIAlertController(title: "Preparing GIF", message: "Please wait...", preferredStyle: .alert)
        present(loadingAlert, animated: true)
        
        // Download the GIF data
        GIFDownloadService.shared.downloadGIF(from: gif.urlString) { data, error in
            DispatchQueue.main.async {
                // Dismiss the loading indicator
                self.dismiss(animated: true) {
                    if let error = error {
                        self.showErrorAlert(error: error)
                        return
                    }
                    
                    guard let gifData = data else {
                        self.showErrorAlert(message: "Failed to download GIF")
                        return
                    }
                    
                    // Create a temporary file URL for the GIF
                    let tempDirectoryURL = URL(fileURLWithPath: NSTemporaryDirectory(), isDirectory: true)
                    let tempFileURL = tempDirectoryURL.appendingPathComponent(UUID().uuidString).appendingPathExtension("gif")
                    
                    do {
                        // Write GIF data to temporary file
                        try gifData.write(to: tempFileURL)
                        
                        // Insert the GIF directly as a standard attachment into the message field
                        conversation.insertAttachment(tempFileURL, withAlternateFilename: "animated.gif") { error in
                            if let error = error {
                                self.showErrorAlert(error: error)
                            } else {
                                // Successfully inserted the attachment
                                self.requestPresentationStyle(.compact)
                            }
                        }
                    } catch {
                        self.showErrorAlert(error: error)
                    }
                }
            }
        }
    }
    
    private func showErrorAlert(error: Error? = nil, message: String? = nil) {
        let errorMessage = message ?? error?.localizedDescription ?? "An unknown error occurred"
        let alertController = UIAlertController(title: "Error", message: errorMessage, preferredStyle: .alert)
        alertController.addAction(UIAlertAction(title: "OK", style: .default))
        present(alertController, animated: true)
    }
    
    // MARK: - Conversation Handling
    
    override func willBecomeActive(with conversation: MSConversation) {
        // Called when the extension is about to move from the inactive to active state.
        // This will happen when the extension is about to present UI.
        
        // Refresh GIFs list when becoming active
        gifCollectionVC?.viewWillAppear(true)
        
        // We don't need to check for custom message URLs anymore since 
        // we're sending standard GIF attachments
    }
    
    override func didResignActive(with conversation: MSConversation) {
        // Called when the extension is about to move from the active to inactive state.
        // This will happen when the user dismisses the extension, changes to a different
        // conversation or quits Messages.
        
        // Use this method to release shared resources, save user data, invalidate timers,
        // and store enough state information to restore your extension to its current state
        // in case it is terminated later.
    }
   
    override func didReceive(_ message: MSMessage, conversation: MSConversation) {
        // Called when a message arrives that was generated by another instance of this
        // extension on a remote device.
        
        // Since we're now sending GIFs as standard attachments rather than
        // custom messages, we don't need special handling for received messages
    }
    
    override func didStartSending(_ message: MSMessage, conversation: MSConversation) {
        // Called when the user taps the send button.
    }
    
    override func didCancelSending(_ message: MSMessage, conversation: MSConversation) {
        // Called when the user deletes the message without sending it.
    
        // Use this to clean up state related to the deleted message.
    }
    
    override func willTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        // Called before the extension transitions to a new presentation style.
    
        // Use this method to prepare for the change in presentation style.
    }
    
    override func didTransition(to presentationStyle: MSMessagesAppPresentationStyle) {
        // Called after the extension transitions to a new presentation style.
    
        // Use this method to finalize any behaviors associated with the change in presentation style.
    }

}
