//
//  DeviceSelectInfoDB.m
//  ATIDDemo
//
//  Created by ATID 
//  Copyright © 2016년 Alluser.net Corp. All rights reserved.
//
#import "DeviceSelectInfoDB.h"


@implementation DeviceSelectInfo

-(void)encodeWithCoder:(NSCoder *)encoder{
    [encoder encodeObject:self.name forKey:@"BTName"];
    [encoder encodeObject:self.address forKey:@"BTAddress"];
}

- (id)initWithCoder:(NSCoder *)decoder {
    if (self = [super init]) {
        self.name = [decoder decodeObjectForKey:@"BTName"];
        self.address = [decoder decodeObjectForKey:@"BTAddress"];
    }
    return self;
}

+ (id)itemWith:(NSString *)name address:(NSString *)address {
    DeviceSelectInfo *item = [[DeviceSelectInfo alloc] init];
    [item setName:name];
    [item setAddress:address];
    return item;
};

@end



@implementation DeviceSelectInfoDB{

     NSMutableArray *tempArray;
}

- (void) SaveBTInfo:(NSString *)name macaddress:(NSString*)address
{
    _info = [[DeviceSelectInfo alloc] init];
    _info.name = name;
    _info.address = address;
    NSMutableArray * testarray;
    testarray = [[NSMutableArray alloc] init];
    
    
    DeviceSelectInfo *item = nil;
    mapDevices = [self getMapData];
    if ((item = (DeviceSelectInfo *)[mapDevices objectForKey:name]) != nil) {
        aryDevices = [self getArrayData];
        
    } else {
        item = [DeviceSelectInfo itemWith:name address:address];
        DeviceSelectInfo *oldinfo = nil;
        aryDevices = [self getArrayData];
//        if(aryDevices.count == 5){
//            oldinfo = [aryDevices objectAtIndex:0];
//            [self DeleteBTInfo:oldinfo.name macaddress:oldinfo.address];
//            aryDevices = [self getArrayData];
//        }
        [aryDevices addObject:item];
        [mapDevices setObject:item forKey:name];
        [testarray addObject:item];
    }
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];
    
    [NSKeyedArchiver archiveRootObject:aryDevices toFile:appFile];
}
- (void) DeleteBTInfo:(NSString *)name macaddress:(NSString*)address
{
    _info = [[DeviceSelectInfo alloc] init];
    _info.name = name;
    _info.address = address;
    NSMutableArray * testarray;
    testarray = [[NSMutableArray alloc] init];
    
    
    DeviceSelectInfo *item = nil;
    mapDevices = [self getMapData];
    aryDevices = [self getArrayData];
    int i;
    for(i=0;i<aryDevices.count;i++){
        DeviceSelectInfo *newItem = [aryDevices objectAtIndex:i];
        if([newItem.name isEqualToString:name]){
            
        }else {
            [testarray addObject:newItem];
        }
//        if ((item = (DeviceSelectInfo *)[mapDevices objectForKey:name]) != nil) {
//            NSLog(@"DeleteBTInfo %@",(DeviceSelectInfo *)[mapDevices objectForKey:name]);
//        } else {
//            DeviceSelectInfo *newItem = [aryDevices objectAtIndex:i];
//            [testarray addObject:newItem];
//        }
    }
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];
    
    [NSKeyedArchiver archiveRootObject:testarray toFile:appFile];
}
- (void) SaveBTLEInfo:(NSString *)name
{
    _info = [[DeviceSelectInfo alloc] init];
    _info.name = name;
 
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];
    
    [NSKeyedArchiver archiveRootObject:_info toFile:appFile];
}
- (NSString *)getBTInfoAddress
{
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];
    
    NSString *address;
    
    DeviceSelectInfo * info;
    info = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    
    DeviceSelectInfo * newInfo;
    if(tempArray != nil){
    newInfo = [aryDevices objectAtIndex:0];
        address = newInfo.address;
    } else {
        address=@"";
    }

    return address;
}

- (NSString *)getBTInfoname
{
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];

    DeviceSelectInfo * info;
    info = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    NSString * name;
    DeviceSelectInfo * newInfo;
    if(tempArray != nil){
        newInfo = [aryDevices objectAtIndex:0];
        name = newInfo.name;
    } else {
        name=@"";
    }
    
    return name;
}

- (void) initArray {
    aryDevices = [[NSMutableArray alloc] init];
    mapDevices = [[NSMutableDictionary alloc] init];
}

- (NSMutableArray *)getArrayData {
    aryDevices = [[NSMutableArray alloc] init];
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];

    //aryDevices = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
   
    tempArray = [[NSMutableArray alloc] init];
    tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    int i;
    if (tempArray == nil){
        return aryDevices;
    }
    if(tempArray != nil){
        if(tempArray.count > 0){
            NSLog(@"");
            for(i=0; i<tempArray.count;i++){
                DeviceSelectInfo *newItem = [tempArray objectAtIndex:i];
                [aryDevices addObject:newItem];
            }
        }
        
    }
    return aryDevices;

}
- (NSMutableDictionary *)getMapData {
    mapDevices = [[NSMutableDictionary alloc] init];
    
    NSArray* docdir = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* docPath = [docdir objectAtIndex:0];
    NSString *appFile = [docPath stringByAppendingPathComponent:@"BTInfo.a"];
    
    //aryDevices = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    
    tempArray = [[NSMutableArray alloc] init];
    tempArray = [NSKeyedUnarchiver unarchiveObjectWithFile:appFile];
    int i;
    if (tempArray == nil){
        return mapDevices;
    }
    if(tempArray != nil){
        if(tempArray.count > 0){
            NSLog(@"");
            for(i=0; i<tempArray.count;i++){
                DeviceSelectInfo *newItem = [tempArray objectAtIndex:i];
                DeviceSelectInfo *item = [DeviceSelectInfo itemWith:newItem.name address:newItem.address];
                [mapDevices setObject:item forKey:newItem.name];
            }
        }
        
    }
    return mapDevices;
    
}
@end
