import * as React from "react";
import { 
  Button
} from "@fluentui/react-components";
import { 
  ShieldDismissRegular
} from "@fluentui/react-icons";
import { useStatusContext } from "./App";
import { useCommonStyles } from "./commonStyles";

export const RemoveConfidential: React.FC = () => {
  const styles = useCommonStyles();
  const { 
    statusMessage, setStatusMessage,
    statusType, setStatusType,
    isProcessing, setIsProcessing
  } = useStatusContext();

  const removeConfidentialMarking = async () => {
    setIsProcessing(true);
    try {
      await PowerPoint.run(async (context) => {
        try {
          // Get all slides in the presentation
          const slides = context.presentation.slides;
          slides.load("items");
          await context.sync();
          
          if (slides.items.length === 0) {
            setStatusMessage("No slides found in the presentation.");
            setStatusType("warning");
            return;
          }
          
          // Process counter
          let processedSlides = 0;
          let errorSlides = 0;
          let removedCount = 0;
          
          // Process each slide
          for (let i = 0; i < slides.items.length; i++) {
            try {
              const slide = slides.items[i];
              
              // Load all shapes on the slide
              slide.shapes.load("items");
              await context.sync();
              
              // Find shapes with name "ConfidentialMarking"
              for (let j = 0; j < slide.shapes.items.length; j++) {
                const shape = slide.shapes.items[j];
                shape.load("name");
                await context.sync();
                
                if (shape.name === "ConfidentialMarking") {
                  // Delete the confidential marking shape
                  shape.delete();
                  removedCount++;
                }
              }
              
              await context.sync();
              processedSlides++;
            } catch (slideError) {
              console.error(`Error processing slide ${i+1}:`, slideError);
              errorSlides++;
              // Continue to the next slide
              continue;
            }
          }
          
          // Report results
          if (removedCount > 0) {
            setStatusMessage(`Removed ${removedCount} confidential markings from ${processedSlides} slides.`);
            setStatusType("success");
          } else if (errorSlides > 0) {
            setStatusMessage(`Failed to remove markings. Errors on ${errorSlides} slides.`);
            setStatusType("error");
          } else {
            setStatusMessage("No confidential markings found to remove.");
            setStatusType("info");
          }
          
        } catch (innerError) {
          console.error("Inner error:", innerError);
          throw innerError; // Re-throw to outer catch
        }
      });
    } catch (error) {
      setStatusMessage(`Error: ${error.message}`);
      setStatusType("error");
      console.error("Remove confidential error:", error);
    } finally {
      setIsProcessing(false);
    }
  };

  return (
    <div className={styles.container}>
      <div className={styles.buttonGroup}>
        <Button 
          appearance="primary" 
          className={styles.actionButton}
          onClick={removeConfidentialMarking} 
          icon={<ShieldDismissRegular />}
          disabled={isProcessing}
        >
          Remove Confidential
        </Button>
      </div>
    </div>
  );
};

export default RemoveConfidential;