//
//  SkillFukugo.m
//  ArmorP3V1
//
#import "SkillFukugo.h"
#import "PSMutex.h"

@implementation SkillFukugo

@synthesize mapFukugo = _mapFukugo;

-(id)init {
    self = [super init];
    if (self) {
        _mapFukugo = [[NSMutableDictionary alloc]init];
    }else {
        [[PSMutex mainMutex] raiseMemoryError];
    }
    [[PSMutex mainMutex] showMemoryAlertIfError];
    return self;
}

-(void)dealloc {
    [_mapFukugo release];
    [super dealloc];
}

-(void)searchFilter:(PSSession *)session: (NSMutableArray*) result: (SkillSet*)skills {
    if (skills == nil) {
        skills = session.searchSkills;
    }
    if (session.searchFukugo == FALSE) {
        [result addObject: skills];
        return;
    }
    
    NSArray* keys = [_mapFukugo allKeys];
    
    for (int i = 0; i < keys.count; ++ i) {
        SkillPoint* point = [keys objectAtIndex:i];
        NSMutableArray* list = [_mapFukugo objectForKey: point];
        
        BOOL mod = FALSE;
        SkillSet* newSet = [[SkillSet alloc]init];
        [newSet set_all:skills];
        
        for (int j = 0; j < list.count; ++ j) {
            SkillPoint* p = [list objectAtIndex:j];
            if (p.positiveRange) {
                SkillKind* kind = p.skillKind;
                int src_point = p.skillPoint;
                
                int x = [newSet findByKind:kind];
                if (x < 0) {
                    continue;
                }
                
                int target_point = newSet.point[x];
                if (src_point >= target_point) {
                    [newSet removeAtIndex: x];
                    [newSet set:point.skillKind :point.skillPoint :point.positiveRange];
                    mod = TRUE;
                }
            }
            if (mod) {
                NSMutableArray* obj = [[NSMutableArray alloc]init];
                [self searchFilter:session :obj :newSet];
                [result addObjectsFromArray: obj];
            }
        }
        
        [newSet release];
    }
    [keys release];
    [result addObject: skills];
}

@end
