/*
	File: ChatClientController.m
	Constains: UI for Bluetooth sample [not to be used as UI sample code]
	Author: Marco Pontil

	Copyright (c) 2002 by Apple Computer, Inc., all rights reserved.
*/
/*
	IMPORTANT:  This Apple software is supplied to you by Apple Computer, Inc. ("Apple") in
	consideration of your agreement to the following terms, and your use, installation, 
	modification or redistribution of this Apple software constitutes acceptance of these 
	terms.  If you do not agree with these terms, please do not use, install, modify or 
	redistribute this Apple software.
	
	In consideration of your agreement to abide by the following terms, and subject to these 
	terms, Apple grants you a personal, non-exclusive license, under AppleÕs copyrights in 
	this original Apple software (the "Apple Software"), to use, reproduce, modify and 
	redistribute the Apple Software, with or without modifications, in source and/or binary 
	forms; provided that if you redistribute the Apple Software in its entirety and without 
	modifications, you must retain this notice and the following text and disclaimers in all 
	such redistributions of the Apple Software.  Neither the name, trademarks, service marks 
	or logos of Apple Computer, Inc. may be used to endorse or promote products derived from 
	the Apple Software without specific prior written permission from Apple. Except as expressly
	stated in this notice, no other rights or licenses, express or implied, are granted by Apple
	herein, including but not limited to any patent rights that may be infringed by your 
	derivative works or by other works in which the Apple Software may be incorporated.
	
	The Apple Software is provided by Apple on an "AS IS" basis.  APPLE MAKES NO WARRANTIES, 
	EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, 
	MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS 
	USE AND OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
	
	IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL OR CONSEQUENTIAL 
	DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 
	OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, 
	REPRODUCTION, MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED AND 
	WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE), STRICT LIABILITY OR 
	OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#import "ChatClientController.h"

#ifdef USE_C_API
#import "ChatBluetoothInterface_C.h"
#else
#import "ChatBluetoothInterface_ObjC.h"
#endif


// NOTE: This is NOT an UI example, the user interface in this application is purposely
// minimal. Do not consider using this file as code sample for your applications.

@implementation ChatClientController

// Object Allocation and Deallocation
@synthesize readmoon_button;
@synthesize sunday_label;
@synthesize sunday_stepper;
@synthesize saturday_label;
@synthesize saturday_stepper;
@synthesize friday_label;
@synthesize friday_stepper;
@synthesize thursday_stepper;
@synthesize wednesday_label;
@synthesize wednesday_stepper;
@synthesize tuesday_label;
@synthesize tuesday_stepper;
@synthesize monday_label;
@synthesize monday_stepper;

@synthesize moonslider_label;
@synthesize mooncycle_button;
@synthesize moonpreview_slider;
@synthesize moon_pic;
@synthesize moonsim_label;
@synthesize label_test_moon;
@synthesize moon_label;
@synthesize moon_bar;
@synthesize red_label;
@synthesize pasek_red;
@synthesize green_label;
@synthesize pasek_green;
@synthesize orange_label;
@synthesize pasek_orange;


@synthesize uv_label;

@synthesize pasek_uv;
@synthesize blue3_label;
@synthesize pasek_blue3;
@synthesize blue2_label;
@synthesize pasek_blue2;
@synthesize blue1_label;
@synthesize pasek_blue1;
@synthesize white_label;
@synthesize pasek_white;
@synthesize testmanual_button;
@synthesize manual_label;
@synthesize setmanual_button;
@synthesize manualslider;
@synthesize t5start;
@synthesize t5end;
@synthesize ledreset;
@synthesize duration_label;
@synthesize savet5button;
@synthesize totaldaylightpower_label;
@synthesize duration_slider;
@synthesize totaldaylightpower_slider;
@synthesize sunrisetimepciker;
@synthesize sunsettimepicker;
@synthesize daysettingsbutton;
@synthesize actualdatetime;
@synthesize setdatetime_button;
@synthesize Systemtime_button;
@synthesize resetlamp_button;
- init
{
    self = [super init];

#ifdef USE_C_API
    myBluetoothInterface = [[ChatBluetoothInterface_C alloc] init];
#else
    myBluetoothInterface = [[ChatBluetoothInterface_ObjC alloc] init];
#endif
    
    // Registes the callbacks for the data and for disconnection:
    [myBluetoothInterface registerForTermination:self action:@selector(chatHandleRemoteDisconnection)];

    // gets the local device name:
    localDeviceName = [myBluetoothInterface localDeviceName];
    [localDeviceName retain];
    
    return self;
}

- (void) dealloc
{
    [localDeviceName release];
    [myBluetoothInterface release];
    [super dealloc];
    //[sunrisetimepciker setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"]autorelease]];
    //[sunsettimepicker setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"en_UK"]autorelease]];
    //[t5start setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
   //[t5end setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
}

- (BOOL) windowShouldClose: (NSWindow*) sender
{
    [myBluetoothInterface disconnectFromServer];
    
    exit(0);
    
    return TRUE;
}

// UI Handlers.
- (IBAction)chatActionOnConnect:(id)sender
{
    if ([myBluetoothInterface connectToServer] == FALSE)
    {
        NSBeep();
        
        [self chatHandleRemoteDisconnection];
    }
    else
    {
        [chatDisconnectButton setEnabled:TRUE];
        [chatInputTextField setEnabled:TRUE];
        [myBluetoothInterface registerForNewData:self action:@selector(chatHandleNewData:)];
    }
}

- (IBAction)chatActionOnDisconnect:(id)sender
{
    [myBluetoothInterface disconnectFromServer];
    [self chatHandleRemoteDisconnection];
}

- (IBAction)chatActionOnMessageTextField:(id)sender
{
    NSRange      theRange;
    unsigned int start;
    NSMutableString    *theString;

    // Send the message trough the bluetooth channel:
    theString = [NSMutableString stringWithFormat:@"%@\r",[chatInputTextField stringValue]];
    
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];

    // Send the message trough the bluetooth channel:
    theString = [NSMutableString stringWithFormat:@" %@", theString];

    // Dump it on the screen
    start = [[chatOutputTextField string] length];
    theRange = NSMakeRange(start, 0 );
    
    [chatOutputTextField replaceCharactersInRange:theRange withString:theString];
    theRange = NSMakeRange(start, [theString length] );
    [chatOutputTextField setTextColor:[NSColor blueColor] range:theRange];
    
    // Clears the input text field:
    [chatInputTextField setStringValue:@""];
}


// Bluetooth Handlers
- (void)chatHandleRemoteDisconnection
{
    [chatDisconnectButton setEnabled:FALSE];
    [chatInputTextField setEnabled:FALSE];
    
    [myBluetoothInterface registerForNewData:nil action:nil];
}

- (void)chatHandleNewData:(NSData*)dataObject
{
    NSRange      theRange;
    unsigned int start;
    NSString    *theString;
    
    // Dump the message on the screen
    start = [[chatOutputTextField string] length];
    theRange = NSMakeRange(start, 0 );
    
    theString = [NSMutableString stringWithFormat:@"%@" , [[[NSString alloc] initWithBytes:[dataObject bytes] length:[dataObject length]] autorelease]];
    
    [chatOutputTextField replaceCharactersInRange:theRange withString:theString];
    theRange = NSMakeRange(start, [theString length] );
    [chatOutputTextField setTextColor:[NSColor redColor] range:theRange];
}

- (IBAction)resetlamp_button_pushed:(id)sender {
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#100\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
}
- (IBAction)systemtime_button_pushed:(id)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    //NSString *theTime; 
    //theTime = [dateFormatter stringFromDate:[moondate_picker dateValue]];
    //datalabel.text = theTime;
    
    NSDate *today = [NSDate date];
    
    
    
    
    
    NSDateFormatter *tempFormatter1 = [[NSDateFormatter alloc]init];
    [tempFormatter1 setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    //NSDate *toDate = [tempFormatter1 dateFromString:@"20-01-2011 09:00:00"];
    // NSDate *toDate = [tempFormatter1 dateFromString:[NSString stringWithFormat:@"%d",theTime]];
    //NSLog(@"toDate ==%@",toDate);
    //toDate = [dateFormatter dateFromString:theTime];
    
    //toDate = [dateFormatter dateFromString:theTime];
    
    //NSDate *today = [NSDate date];
    
    [actualdatetime setDateValue:[NSDate date]];
    NSLog(@"dzis jest ==%@",today);
}
- (IBAction)setdatetimebutton_pushed:(id)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    NSString *theTime; 
    theTime = [dateFormatter stringFromDate:[actualdatetime dateValue]];
    //datalabel.text = theTime;
    
    NSCalendar *calendar = [NSCalendar currentCalendar];
    int hour   =    [[calendar components:NSHourCalendarUnit    fromDate:[actualdatetime dateValue]] hour];
    int minute =    [[calendar components:NSMinuteCalendarUnit  fromDate:[actualdatetime dateValue]] minute];
    
    int year   =    [[calendar components:NSYearCalendarUnit    fromDate:[actualdatetime dateValue]] year];
    int month  =    [[calendar components:NSMonthCalendarUnit   fromDate:[actualdatetime dateValue]] month];
    int day    =    [[calendar components:NSDayCalendarUnit     fromDate:[actualdatetime dateValue]] day];
    
    
    NSString *source = theTime;
    
    
    NSRange dniRange = NSMakeRange(0, 2);
    NSString *dni = [source substringWithRange:dniRange];
    NSRange miesiacRange = NSMakeRange(3, 2);
    NSString *miesiac = [source substringWithRange:miesiacRange];
    NSRange rokRange = NSMakeRange(6, 4);
    NSString *rok = [source substringWithRange:rokRange];
    NSRange godzinaRange = NSMakeRange(11, 2);
    NSString *godzina = [source substringWithRange:godzinaRange];
    NSRange minutaRange = NSMakeRange(14, 2);
    NSString *minuta = [source substringWithRange:minutaRange];
    
    
    
    NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
    [tempFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    //NSDate *startdate = [tempFormatter dateFromString:@"01-01-2000  01:00:00"];
    NSLog(@"theTime ==%@",source);
    
    //NSDateFormatter *tempFormatter1 = [[NSDateFormatter alloc]init];
    //[tempFormatter1 setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    //NSDate *toDate = [tempFormatter1 dateFromString:@"20-01-2011 09:00:00"];
    // NSDate *toDate = [tempFormatter1 dateFromString:[NSString stringWithFormat:@"%d",theTime]];
    NSLog(@"dzien = %@",dni);
    NSLog(@"miesiac = %@",miesiac);
    NSLog(@"rok = %@",rok);
    
    NSLog(@"godzina = %@",godzina);
    NSLog(@"minuta = %@",minuta);
    // NSLog(@"rok ==%@",rok);
    
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#3\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
    
    int actualrok = year - 2000;
    
    NSString *theString2 = [NSString stringWithFormat:@"%d", actualrok];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
    
    NSString *theString4 = [NSString stringWithFormat:@"%d", month];
    [myBluetoothInterface sendData:(void*)[theString4 UTF8String] length:[theString4 length]];
    
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSString *theString6 = [NSString stringWithFormat:@"%d", day];
    [myBluetoothInterface sendData:(void*)[theString6 UTF8String] length:[theString6 length]];
    
    NSMutableString    *theString7;
    theString7 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString7 UTF8String] length:[theString7 length]];
    
    NSString *theString8 = [NSString stringWithFormat:@"%d", hour];
    [myBluetoothInterface sendData:(void*)[theString8 UTF8String] length:[theString8 length]];
    
    NSMutableString    *theString9;
    theString9 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString9 UTF8String] length:[theString9 length]];
    
    NSString *theString10 = [NSString stringWithFormat:@"%d", minute];
    [myBluetoothInterface sendData:(void*)[theString10 UTF8String] length:[theString10 length]];
    
    NSMutableString    *theString11;
    theString11 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString11 UTF8String] length:[theString11 length]];
    
    int secund = 0;
    
    NSString *theString12 = [NSString stringWithFormat:@"%d", secund];
    [myBluetoothInterface sendData:(void*)[theString12 UTF8String] length:[theString12 length]];
    
    NSMutableString    *theString13;
    theString13 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString13 UTF8String] length:[theString13 length]];
    
    // toDate = [dateFormatter dateFromString:theTime];
}
- (IBAction)daysettingsbutton_pushed:(id)sender {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init]; 
   // [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
   // [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    //NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];
    //[dateFormatter setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
    //[sunrisetimepciker setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
   // [sunsettimepicker setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
   // [dateFormatter setLocale:locale];
    //[locale release];
     //[dateFormatter setDateFormat:@"HH:mm"];
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    
    NSString *SunriseTime; 
    SunriseTime = [dateFormatter stringFromDate:[sunrisetimepciker dateValue]];
    NSString *SunSetTime; 
    SunSetTime = [dateFormatter stringFromDate:[sunsettimepicker dateValue]];
    
    //NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
    //[tempFormatter setDateFormat:@"HH:mm"];
    
    // Zamiana czasu wschodu na minuty
    NSString *source = SunriseTime;
    
    NSRange godzinaRange = NSMakeRange(11, 2);
    NSString *godzina = [source substringWithRange:godzinaRange];
    NSRange minutaRange = NSMakeRange(14, 2);
    NSString *minuta = [source substringWithRange:minutaRange];
    
    NSLog(@"LED sunrise ==%@",SunriseTime);
    NSLog(@"LED sunset ==%@",SunSetTime);
    //NSLog(@"LED sunrise hour ==%@",godzina);
    // NSLog(@"LED sunrise minutes  ==%@",minuta);
    
    int godzinawart = [godzina intValue];
    int minutawart = [minuta intValue];
    
    int liczbaminut = godzinawart * 60;
    int totalsunrise = liczbaminut + minutawart;
    NSString *sunrisestring =  [NSString stringWithFormat:@"%i", totalsunrise];
    
    NSLog(@"Total sunrise minute = %@", sunrisestring ) ;
    
    // Zamiana czasu zachodu na minuty
    NSString *source2 = SunSetTime;
    
    
    
    NSRange godzinaRange2 = NSMakeRange(11, 2);
    NSString *godzina2 = [source2 substringWithRange:godzinaRange2];
    NSRange minutaRange2 = NSMakeRange(14, 2);
    NSString *minuta2 = [source2 substringWithRange:minutaRange2];
    
    
    //NSLog(@"LED sunset ==%@",SunSetTime);
    //NSLog(@"LED sunrise hour ==%@",godzina2);
    // NSLog(@"LED sunset minutes  ==%@",minuta2);
    
    int godzinawart2 = [godzina2 intValue];
    int minutawart2 = [minuta2 intValue];
    
    int liczbaminut2 = godzinawart2 * 60;
    int totalsunset = liczbaminut2 + minutawart2;
    NSString *sunsetstring =  [NSString stringWithFormat:@"%i", totalsunset];
    
    
    
    NSLog(@"Total sunset minute = %@", sunsetstring ) ;
    
    // wysylanie ustawien LED do lampy
    //sunrise
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#2\r4\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
    
    NSString *theString2 = [NSString stringWithFormat:@"%d",[sunrisestring intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
    
    //sunset
    NSMutableString    *theString4;
    theString4 = [NSMutableString stringWithFormat:@"#2\r6\r"];
    [myBluetoothInterface sendData:(void*)[theString4 UTF8String] length:[theString4 length]];
    
    NSString *theString5 = [NSString stringWithFormat:@"%d",[sunsetstring intValue]];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString6;
    theString6 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString6 UTF8String] length:[theString6 length]];
    

}
- (IBAction)totaldaylightpowerslider_changed:(id)sender {
    [totaldaylightpower_label setIntValue:[totaldaylightpower_slider intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r8\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [totaldaylightpower_slider intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
}
- (IBAction)durationslider_changed:(id)sender {
    [duration_label setIntValue:[duration_slider intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r18\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    int durationproper = [duration_slider intValue] * 60 ;
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", durationproper];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
}
- (IBAction)savet5pushed:(id)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    //[dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    //[dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    //[t5start setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
   // [t5end setLocale:[[[NSLocale alloc] initWithLocaleIdentifier:@"pl_PL"]autorelease]];
    
    NSString *t5starttime; 
    t5starttime = [dateFormatter stringFromDate:[t5start dateValue]];
    NSString *t5endtime; 
    t5endtime = [dateFormatter stringFromDate:[t5end dateValue]];
    
    //NSDateFormatter *tempFormatter = [[NSDateFormatter alloc]init];
  //  [tempFormatter setDateFormat:@"HH:mm"];
    
    // Zamiana czasu wschodu na minuty
    NSString *source = t5starttime;
    
    NSRange godzinaRange = NSMakeRange(11, 2);
    NSString *godzina = [source substringWithRange:godzinaRange];
    NSRange minutaRange = NSMakeRange(14, 2);
    NSString *minuta = [source substringWithRange:minutaRange];
    
    //NSRange godzinaRange = NSMakeRange(0, 2);
   // NSString *godzina = [source substringWithRange:godzinaRange];
   // NSRange minutaRange = NSMakeRange(3, 2);
    //NSString *minuta = [source substringWithRange:minutaRange];
    
    NSLog(@"t5 sunrise ==%@",t5starttime);
    NSLog(@"t5 sunset ==%@",t5endtime);
    //NSLog(@"LED sunrise hour ==%@",godzina);
    // NSLog(@"LED sunrise minutes  ==%@",minuta);
    
    int godzinawart = [godzina intValue];
    int minutawart = [minuta intValue];
    
    int liczbaminut = godzinawart * 60;
    int totalsunrise = liczbaminut + minutawart;
    NSString *t5startstring =  [NSString stringWithFormat:@"%i", totalsunrise];
    
    NSLog(@"Total t5 start minute = %@", t5startstring ) ;
    
    // Zamiana czasu zachodu na minuty
    NSString *source2 = t5endtime;
    
    
    
    NSRange godzinaRange2 = NSMakeRange(11, 2);
    NSString *godzina2 = [source2 substringWithRange:godzinaRange2];
    NSRange minutaRange2 = NSMakeRange(14, 2);
    NSString *minuta2 = [source2 substringWithRange:minutaRange2];
    
    
    //NSLog(@"LED sunset ==%@",SunSetTime);
    //NSLog(@"LED sunrise hour ==%@",godzina2);
    // NSLog(@"LED sunset minutes  ==%@",minuta2);
    
    int godzinawart2 = [godzina2 intValue];
    int minutawart2 = [minuta2 intValue];
    
    int liczbaminut2 = godzinawart2 * 60;
    int totalsunset = liczbaminut2 + minutawart2;
    NSString *t5endstring =  [NSString stringWithFormat:@"%i", totalsunset];
    
    
    
    NSLog(@"Total t5 end minute = %@", t5endstring ) ;
    
    // wysylanie ustawien LED do lampy
    //sunrise
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#2\r1100\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
    
    NSString *theString2 = [NSString stringWithFormat:@"%d",[t5startstring intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
    
    //sunset
    NSMutableString    *theString4;
    theString4 = [NSMutableString stringWithFormat:@"#2\r1102\r"];
    [myBluetoothInterface sendData:(void*)[theString4 UTF8String] length:[theString4 length]];
    
    NSString *theString5 = [NSString stringWithFormat:@"%d",[t5endstring intValue]];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString6;
    theString6 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString6 UTF8String] length:[theString6 length]];
    

}
- (IBAction)ledreset_pushed:(id)sender {
    NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; 
    [dateFormatter setDateStyle:NSDateFormatterShortStyle]; 
    [dateFormatter setTimeStyle:NSDateFormatterShortStyle]; 
    //NSString *theTime; 
    //theTime = [dateFormatter stringFromDate:[moondate_picker dateValue]];
    //datalabel.text = theTime;
    
    //NSDate *today = [NSDate date];
    
    
    
    
    
    NSDateFormatter *tempFormatter1 = [[NSDateFormatter alloc]init];
    [tempFormatter1 setDateFormat:@"dd-MM-yyyy HH:mm:ss"];
    NSDate *toDate = [tempFormatter1 dateFromString:@"20-01-2011 09:00:00"];
    NSDate *toDate2 = [tempFormatter1 dateFromString:@"20-01-2011 16:00:00"];
    // NSDate *toDate = [tempFormatter1 dateFromString:[NSString stringWithFormat:@"%d",theTime]];
    //NSLog(@"toDate ==%@",toDate);
    //toDate = [dateFormatter dateFromString:theTime];
    
    //toDate = [dateFormatter dateFromString:theTime];
    
    //NSDate *today = [NSDate date];
    
    [sunrisetimepciker setDateValue:toDate];
    [sunrisetimepciker setDateFormat:@"HH:mm"];
     [sunsettimepicker setDateFormat:@"HH:mm"];
    [sunsettimepicker setDateValue:toDate2];
    [totaldaylightpower_label setIntValue:80];
    [totaldaylightpower_slider setIntValue:80];
    [duration_label setIntValue:180];
    [duration_slider setIntValue:180];
    
    
    NSLog(@"dzis jest ==%@",toDate);

}
- (IBAction)manualslider_pushed:(id)sender {
    [manual_label setIntValue:[manualslider intValue]];
}
- (IBAction)setmanualbutton_pushed:(id)sender {
    //[totaldaylightpower_label setIntValue:[totaldaylightpower_slider intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r920\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [manualslider intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
}
- (IBAction)testmanual_pushed:(id)sender {
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#202\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
   
}
- (IBAction)white_change:(id)sender {
    [white_label setIntValue:[pasek_white intValue]];
    
    
    
    // Send the message trough the bluetooth channel:
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#2\r912\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_white intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)blue1_change:(id)sender {
    [blue1_label setIntValue:[pasek_blue1 intValue]];
    
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r914\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_blue1 intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)blue2_change:(id)sender {
    [blue2_label setIntValue:[pasek_blue2 intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r916\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_blue2 intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)blue3_change:(id)sender {
    [blue3_label setIntValue:[pasek_blue3 intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r910\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_blue3 intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)uv_change:(id)sender {
    [uv_label setIntValue:[pasek_uv intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r902\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_uv intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)orange_change:(id)sender {
    [orange_label setIntValue:[pasek_orange intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r904\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_orange intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)green_change:(id)sender {
    [green_label setIntValue:[pasek_green intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r908\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_green intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)red_change:(id)sender {
    [red_label setIntValue:[pasek_red intValue]];
    NSMutableString    *theString5;
    theString5 = [NSMutableString stringWithFormat:@"#2\r906\r"];
    [myBluetoothInterface sendData:(void*)[theString5 UTF8String] length:[theString5 length]];
    
    NSMutableString    *theString2;
    theString2 = [NSString stringWithFormat:@"%d", [pasek_red intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];

}
- (IBAction)moonbar_change:(id)sender {
    [moon_label setIntValue:[moon_bar intValue]];
    
    NSMutableString    *theString;
    theString = [NSMutableString stringWithFormat:@"#2\r16\r"];
    [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
    
    
    NSString *theString2 = [NSString stringWithFormat:@"%d",[moon_bar intValue]];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
    
    NSMutableString    *theString3;
    theString3 = [NSMutableString stringWithFormat:@"\r"];
    [myBluetoothInterface sendData:(void*)[theString3 UTF8String] length:[theString3 length]];
    

}
- (IBAction)moonpreviewslider_changed:(id)sender {
    //  NSImage *moon = [NSImage imageNamed:@"your_image.tif"];
    
    //[NSString stringWithFormat:@"%1.0f", [moondaybar value]]
    [moonslider_label setIntValue: [moonpreview_slider intValue]];
    //[buttonslider_test setValue:[NSImage imageNamed:[NSString stringWithFormat:@"%d",[moonslider_label intValue]]]];
    NSImage *moon = [NSImage imageNamed:[NSString stringWithFormat:@"%d",[moonslider_label intValue]]];
    [moon_pic setImage: moon];
    // NSImage *moon = [NSImage imageNamed:[NSString stringWithFormat:@"%1.0f", [moonpreview_slider intValue]]];
    // [moon_pic setImage: moon];
    int value = [moon_label intValue];
    int a = [moonpreview_slider intValue];
    
    if (a == 1) {
        value = value / 1;
        
    }
    if (a == 2) {
        value = (value*0.92);
        
    } 
    if (a == 3) {
        value = (value*0.86);
        
    } 
    if (a == 4) {
        value = (value*0.74);
        
    } 
    if (a == 5) {
        value = (value*0.66);
        
    } 
    if (a == 6) {
        value = (value*0.57);
        
    } 
    if (a == 7) {
        value = (value*0.52);
        
    } 
    if (a == 8) {
        value = (value*0.46);
        
    } 
    if (a == 9) {
        value = (value*0.39);
        
    } 
    if (a == 10) {
        value = (value*0.32);
        
    } 
    if (a == 11) {
        value = (value*0.25);
        
    } 
    if (a == 12) {
        value = (value*0.18);
        
    } 
    if (a == 13) {
        value = (value*0.12);
        
    } 
    if (a == 14) {
        value = (value*0.06);
        
    } 
    if (a == 15) {
        value = (value *0.00);
        
    } 
    if (a == 16) {
        value = (value*0.06);
        
    } 
    if (a == 17) {
        value = (value*0.12);
        
    } 
    if (a == 18) {
        value = (value*0.18);
        
    } 
    if (a == 19) {
        value = (value*0.25);
        
    } 
    if (a == 20) {
        value = (value*0.32);
        
    } 
    if (a == 21) {
        value = (value*0.39);
        
    } 
    if (a == 22) {
        value = (value*0.45);
        
    } 
    if (a == 23) {
        value = (value*0.53);
        
    } 
    if (a == 24) {
        value = (value*0.60);
        
    } 
    if (a == 25) {
        value = (value*0.67);
        
    } 
    if (a == 26) {
        value = (value*0.75);
        
    } 
    if (a == 27) {
        value = (value*0.82);
        
    } 
    if (a == 28) {
        value = (value*0.89);
        
    } 
    if (a == 29) {
        value = (value *0.97);
        
    } 
    
    
    
    [moonsim_label setIntValue:value];
    

}
- (IBAction)mooncyclebutton_changed:(id)sender {
    int wynik = [[NSSegmentedControl alloc], mooncycle_button selectedSegment ]; 
    [label_test_moon setIntValue:wynik];
    
    switch (wynik) 
    {
        case 0:
        {
            
            NSMutableString    *theString;
            theString = [NSMutableString stringWithFormat:@"#2\r14\r1\r"];
            [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
            
            
        }  
            break;
            
        case 1:
        {
            NSMutableString    *theString;
            theString = [NSMutableString stringWithFormat:@"#2\r14\r0\r"];
            [myBluetoothInterface sendData:(void*)[theString UTF8String] length:[theString length]];
            
        }
            
            break;
            
        default:
            break;
            
            
    }

}
- (IBAction)monday_clicked:(id)sender {
     NSLog(@"monday =%@",[mondaystepper intValue]);
}
- (IBAction)monday_stepper_changed:(id)sender {
   [ monday_label setIntValue: [ monday_stepper intValue]] ;
}
- (IBAction)tuesday_stepper_changed:(id)sender {
     [ tuesday_label setIntValue: [ tuesday_stepper intValue]] ;
}
- (IBAction)wednesday_stepper_changed:(id)sender {
     [ wednesday_label setIntValue: [ wednesday_stepper intValue]] ;
}
- (IBAction)thursday_stepper_changed:(id)sender {
     [ thursday_label setIntValue: [ thursday_stepper intValue]] ;
}
- (IBAction)friday_stepper_changed:(id)sender {
     [ friday_label setIntValue: [ friday_stepper intValue]] ;
}
- (IBAction)saturday_stepper_changed:(id)sender {
     [ saturday_label setIntValue: [ saturday_stepper intValue]] ;
}
- (IBAction)sunday_stepper_changed:(id)sender {
     [ sunday_label setIntValue: [ sunday_stepper intValue]] ;
}
- (IBAction)readmoon_pushed:(id)sender {
    
    NSMutableString    *theString2;
    theString2 = [NSMutableString stringWithFormat:@"#1\r16\r"];
    [myBluetoothInterface sendData:(void*)[theString2 UTF8String] length:[theString2 length]];
     NSLog(@"wysylamy =%@",theString2);
    
    
   NSString    *theString3 = nil;
    theString3 = [myBluetoothInterface  remoteDeviceName]; 
   NSLog(@"nazwa urzadzenia =%@",theString3);
    //[myBluetoothInterface disconnectFromServer];
    
    NSString    *theString4 = nil;
    //NSString *data;
  //  theString4 = [myBluetoothInterface b];
    NSLog(@"odbieramy =%@",theString4);
}
@end
