//
//  LegacyUtilities.m
//  masterkey
//
//  Created by Ian Kohl on 4/6/19.
//  Copyright © 2019 One Sky IT. All rights reserved.
//

#import <Cocoa/Cocoa.h>
#include <Carbon/Carbon.h>

#import "LegacyUtilities.h"

@implementation LegacyUtilities : NSObject

-(void) setupMirror {
    CGDisplayCount numberOfActiveDspys;
    CGDisplayCount numberOfOnlineDspys;
    
    CGDisplayCount numberOfTotalDspys = 2; // The number of total displays
    
    CGDirectDisplayID activeDspys[] = {0,0};
    CGDirectDisplayID onlineDspys[] = {0,0};
    CGDirectDisplayID secondaryDspy;
    
    CGDisplayErr activeError = CGGetActiveDisplayList (numberOfTotalDspys,activeDspys,&numberOfActiveDspys);
    
    if (activeError!=0) NSLog(@"Error in obtaining active diplay list: %d\n",activeError);
        
        CGDisplayErr onlineError = CGGetOnlineDisplayList (numberOfTotalDspys,onlineDspys,&numberOfOnlineDspys);
        
        if (onlineError!=0) NSLog(@"Error in obtaining online diplay list: %d\n",onlineError);
            
            CGDisplayConfigRef configRef;
    CGError err = CGBeginDisplayConfiguration (&configRef);
    if (err != 0) NSLog(@"Error with CGBeginDisplayConfiguration: %d\n",err);
        
        if (numberOfOnlineDspys==2) {
            
            if (onlineDspys[0]==CGMainDisplayID()){
                secondaryDspy = onlineDspys[1];
            } else {
                secondaryDspy = onlineDspys[0];
            }
            
            CGDisplayConfigRef configRef;
            CGError err = CGBeginDisplayConfiguration (&configRef);
            if (err != 0) NSLog(@"Error with CGBeginDisplayConfiguration: %d\n",err);
            
            if (numberOfActiveDspys==2) { // Displays are unmirrored -> mirror them
                err = CGConfigureDisplayMirrorOfDisplay (configRef,secondaryDspy,CGMainDisplayID());
                NSLog(@"ConfigureMirror Error: %d",err);
            }
            err = CGCompleteDisplayConfiguration (configRef,kCGConfigurePermanently);
            NSLog(@"Mirror CompleteConfig Error: %d",err);
            
        } else {
            if (numberOfOnlineDspys>2) {
                printf("Cannot handle more than 2 displays at this time. %d displays detected.\n",numberOfOnlineDspys);
            } else {
                printf("No secondary display detected.\n");
            }
        }
}

-(void) doMirror {
    NSEvent *thisEvent = [NSEvent otherEventWithType:NSEventTypeSystemDefined location:CGPointMake(0,0) modifierFlags:0xa00 timestamp:0 windowNumber:0 context:0 subtype:8 data1:(NX_KEYTYPE_VIDMIRROR<<16|(0xa <<8)) data2:-1];
    CGEventRef cThisEvent = [thisEvent CGEvent];
    CGEventPost(0, (cThisEvent));
    
    NSEvent *thatEvent = [NSEvent otherEventWithType:NSEventTypeSystemDefined location:NSMakePoint(0,0) modifierFlags:0xb00 timestamp:0 windowNumber:0 context:[NSGraphicsContext currentContext] subtype:8 data1:(NX_KEYTYPE_VIDMIRROR<<16|(0xb <<8)) data2:-1];
    CGEventRef cThatEvent = [thatEvent CGEvent];
    CGEventPost(0, (cThatEvent));
}

-(void) undoMirror {
    enum MirrorMode {
        off,
    } mode;
    
    mode = off;
    
    CGDisplayCount numberOfActiveDspys;
    CGDisplayCount numberOfOnlineDspys;
    
    CGDisplayCount numberOfTotalDspys = 2;
    
    CGDirectDisplayID activeDspys[] = {0,0};
    CGDirectDisplayID onlineDspys[] = {0,0};
    CGDirectDisplayID secondaryDspy;
    
    CGDisplayErr activeError = CGGetActiveDisplayList (numberOfTotalDspys,activeDspys,&numberOfActiveDspys);
    
    if (activeError!=0) NSLog(@"Error in obtaining active diplay list: %d\n",activeError);
    
    CGDisplayErr onlineError = CGGetOnlineDisplayList (numberOfTotalDspys,onlineDspys,&numberOfOnlineDspys);
    
    if (onlineError!=0) NSLog(@"Error in obtaining online diplay list: %d\n",onlineError);
    
    if (numberOfOnlineDspys==2) { // Online displays = physical displays regardless of mirror status
        
        if (onlineDspys[0]==CGMainDisplayID()){
            secondaryDspy = onlineDspys[1];
        } else {
            secondaryDspy = onlineDspys[0];
        }
        
        switch (mode) {
            case off:
                if (numberOfActiveDspys!=2) // Active displays = software displays (mirror = 1)
                    [self executeMirrorAction];
                break;
            default:
                break;
        }
    } else {
        if (numberOfOnlineDspys>2) {
            printf("Cannot handle more than 2 displays at this time. %d displays detected.\n",numberOfOnlineDspys);
        } else {
            printf("No secondary display detected.\n");
        }
    }
}

-(void) executeMirrorAction {
    NSEvent *thisEvent = [NSEvent otherEventWithType:NSEventTypeSystemDefined location:CGPointMake(0,0) modifierFlags:0xa00 timestamp:0 windowNumber:0 context:0 subtype:8 data1:(NX_KEYTYPE_VIDMIRROR<<16|(0xa <<8)) data2:-1];
    CGEventRef cThisEvent = [thisEvent CGEvent];
    CGEventPost(0, (cThisEvent));
    
    NSEvent *thatEvent = [NSEvent otherEventWithType:NSEventTypeSystemDefined location:NSMakePoint(0,0) modifierFlags:0xb00 timestamp:0 windowNumber:0 context:[NSGraphicsContext currentContext] subtype:8 data1:(NX_KEYTYPE_VIDMIRROR<<16|(0xb <<8)) data2:-1];
    CGEventRef cThatEvent = [thatEvent CGEvent];
    CGEventPost(0, (cThatEvent));
}

@end
