//
//  VankeSettlementListAPI.m
//  vanke
//
//  Created by xiaomi on 15/10/8.
//  Copyright © 2015年 gomore. All rights reserved.
//

#import "VankeSettlementListAPI.h"
#import "VankeConfig.h"
#import "VankeUtil.h"

@implementation VankeSettlementListAPI

@synthesize settleEquals = _settleEquals;
@synthesize confirmed = _confirmed;
@synthesize projectsIn = _projectsIn;
@synthesize shopNameOrCodeLike = _shopNameOrCodeLike;

-(void) route:(BeeMessage *)msg {
    if (self.sending)
    {
        //        if (![self checkToQuery]) {
        //            return;
        //        }
        
        //        [self http_get:@"/statement/query"].PARAM(@"settleEquals", self.settleEquals).PARAM(@"prjectsIn", [self.projectsIn componentsJoinedByString:@";"]).TIMEOUT(10);
        [self HTTP_GET:@"http://www.baidu.com"].TIMEOUT(10);
    }
    else if (self.succeed)
    {
        NSLog(@"response: %@", self.responseString);
        
        //        self.serverResp = [StatementListResponse objectFromDictionary: self.responseJSONDictionary];
        //        if (nil == self.serverResp)
        //        {
        //            self.failed = YES;
        //            return;
        //        }
        
        StatementListResponse *resp = [StatementListResponse fakeData];
        resp.settle = self.settleEquals;
        self.serverResp = resp;
    } else if (self.failed) {
        NSLog(@"response: %@", self.responseString);
        self.serverResp = [StatementListResponse objectFromDictionary: self.responseJSONDictionary];
    }
}

-(BOOL) checkToQuery {
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    if ([VankeUtil isBlankString:[dateFormatter stringFromDate:self.settleEquals]]) {
        [self setClientError: @"账期不能为空"];
        return NO;
    }
    
    if (nil == self.projectsIn || [self.projectsIn count] <= 0) {
        [self setClientError: @"项目列表不能为空"];
        return NO;
    }
    
    return YES;
}

@end

#pragma Paging

@implementation Paging

@synthesize page = _page;
@synthesize pageSize = _pageSize;
@synthesize pageCount = _pageCount;
@synthesize recordCount = _recordCount;

@end


#pragma SubjectItem

@implementation SubjectItem

@synthesize subject = _subject;
@synthesize beginDate = _beginDate;
@synthesize endDate = _endDate;
@synthesize direction = _direction;
@synthesize amount = _amount;
@synthesize remark = _remark;

@end

#pragma  StatementShop

@implementation StatementShop

@synthesize contract = _contract;
@synthesize shopCode = _shopCode;
@synthesize shopName = _shopName;
@synthesize picture = _picture;
@synthesize settle = _settle;
@synthesize amount = _amount;
@synthesize state = _state;
@synthesize items = _items;

CONVERT_PROPERTY_CLASS(items, SubjectItem)

-(NSString*) getPictureUrlOrDefault {
    return @"bill.png";
//    if ([VankeUtil isBlankString:self.picture]) {
//        return @"bill.png";
//    } else {
//        return [NSString stringWithFormat:@"%@/%@", VANKE_SERVER_MEDIA_BASE_URL, self.picture];
//    }
}

-(NSString*) codeName {
    return [NSString stringWithFormat:@"%@ %@", _shopCode, _shopName];
}

@end

#pragma StatementListResponse

@implementation StatementListResponse

@synthesize settle =  _settle;
@synthesize amount = _amount;
@synthesize paging = _paging;
@synthesize records = _records;

CONVERT_PROPERTY_CLASS(records, StatementShop)

-(BOOL) hasMore {
    return _paging && _paging.pageSize <= _paging.recordCount;
}

#define ARC4RANDOM_MAX      0x100000000

+(NSNumber*) randomDouble {
    double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 10000.0f);
    return [NSNumber numberWithDouble:val];
}

+(NSNumber*) randomInt {
    double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 10000.0f);
    return [NSNumber numberWithInt:val];
}

+(instancetype) fakeData {
    StatementListResponse *resp = [[StatementListResponse alloc] init];
    resp.amount = [self randomDouble];
    
    Paging *paging = [[Paging alloc] init];
    paging.page = [NSNumber numberWithInt:1];
    paging.pageSize = [NSNumber numberWithInt:10];
    paging.pageCount = [NSNumber numberWithInt:10];
    paging.recordCount = [NSNumber numberWithInt:11];
    resp.paging = paging;
    
    NSMutableArray *shops = [[NSMutableArray alloc] initWithCapacity:10];
    for (int i = 0; i < [paging.pageSize intValue]; ++i) {
        StatementShop *summary = [[StatementShop alloc] init];
        summary.contract = @"112233";
        summary.shopCode = [NSString stringWithFormat:@"%03d", i];
        summary.shopName = @"肯得起";
        summary.picture = @"";
        summary.settle = [NSDate date];
        double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 10000.0f);
        summary.amount = [NSNumber numberWithDouble:val];
        
        val = arc4random() % 100;
        if (val > 30) {
            summary.state = @"confirmed";
        } else if (val > 60) {
            summary.state = @"unconfirmed";
        } else {
            summary.state = @"rejected";
        }
        
        NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:10];
        for( int i = 0; i < 100; ++i) {
            SubjectItem *item = [[SubjectItem alloc] init];
            item.subject = @"月固定租金";
            item.beginDate = @"2015-12-01";
            item.endDate = @"2015-12-30";
            item.direction = [NSNumber numberWithInt:0];
            
            val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 10000.0f);
            if (arc4random() % 100 > 50) {
                val = -val;
            }
            item.amount = [NSNumber numberWithDouble:val];
            item.remark = @"有意见，不同意";
            [items addObject:item];
        }
        summary.items = items;
        
        [shops addObject:summary];
    }
    
    resp.records = shops;
    return resp;
}


@end
