//
//  TaskAreaViewController.m
//  Macalline
//
//  Created by JianjiYuan on 14-12-4.
//  Copyright (c) 2014年 JianjiYuan. All rights reserved.
//

#import "TaskAreaViewController.h"
#import "TaskPointViewController.h"

@interface TaskAreaViewController ()

@property (nonatomic, strong) UIButton *btnUncheck;
@property (nonatomic, strong) UIButton *btnChecked;

@property (nonatomic, strong) UIView *viewPointItem;

@property (nonatomic, strong) UIButton *btnCenter;
@property (nonatomic, strong) UIButton *btnLobby;
@property (nonatomic, strong) UIButton *btnAtrium;
@property (nonatomic, strong) UIButton *btnPassage;
@property (nonatomic, strong) UIButton *btnEscalator;

//@property (nonatomic, strong) TaskPointItem *itemCenter;
//@property (nonatomic, strong) TaskPointItem *itemLobby;
//@property (nonatomic, strong) TaskPointItem *itemAtrium;
//@property (nonatomic, strong) TaskPointItem *itemPassage;
//@property (nonatomic, strong) TaskPointItem *itemEscalator;

@end

@implementation TaskAreaViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.title = @"检查点分类";
    
    /*
    NSArray *arrItems = @[self.itemCenter, self.itemLobby, self.itemAtrium, self.itemPassage];
    [self.itemCenter autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.itemCenter autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.itemCenter autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.btnUncheck];
    [arrItems autoDistributeViewsAlongAxis:ALAxisVertical alignedTo:ALAttributeLeft withFixedSpacing:0];
    
    [self.itemLobby autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.itemAtrium autoSetDimension:ALDimensionWidth toSize:160];
    [self.itemPassage autoSetDimension:ALDimensionWidth toSize:160];
    
    [self.itemEscalator autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.itemEscalator autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [self.itemEscalator autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.itemLobby];
    [self.itemEscalator autoSetDimension:ALDimensionWidth toSize:160];
    
    
    _itemCenter.lblName.text = @"外广场 CENTER";
    _itemLobby.lblName.text = @"商场大堂 LOBBY";
    _itemAtrium.lblName.text = @"中庭 \nATRIUM";
    _itemPassage.lblName.text = @"过道 \nPASSAGE";
    _itemEscalator.lblName.text = @"其他 \nESCALATOR";
     
     */
    if (!self.isShowHistory) {
        if ([[DataMgr getInstance].roleId isEqualToString:@"2"]) {
            [self.btnUncheck setTitle:[NSString stringWithFormat:@"未上报(0)"] forState:UIControlStateNormal];
            [self.btnChecked setTitle:[NSString stringWithFormat:@"已上报(0)"] forState:UIControlStateNormal];
        }else {
            [self.btnUncheck setTitle:[NSString stringWithFormat:@"已上报(0)"] forState:UIControlStateNormal];
            [self.btnChecked setTitle:[NSString stringWithFormat:@"已检查(0)"] forState:UIControlStateNormal];
        }
        
    }
    
    [self setViewLayoutConstraint];
    
    [self requestDatas];

}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    
    if ([[UIApplication getObjectType:Number key:kShouldRequestDataWhenBack] boolValue]) {
        [self requestDatas];
    }
}

- (void)setViewLayoutConstraint
{
    if (!self.isShowHistory) {
        [self.btnUncheck autoPinEdgeToSuperviewEdge:ALEdgeTop];
        [self.btnUncheck autoPinEdgeToSuperviewEdge:ALEdgeLeft];
        [self.btnUncheck autoSetDimensionsToSize:CGSizeMake(160, 60)];
        
        [self.btnChecked autoPinEdgeToSuperviewEdge:ALEdgeTop];
        [self.btnChecked autoPinEdgeToSuperviewEdge:ALEdgeRight];
        [self.btnChecked autoSetDimensionsToSize:CGSizeMake(160, 60)];
        
        [self.viewPointItem autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
        [self.viewPointItem autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.btnUncheck];
    }else {
        
        [self.viewPointItem autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    }
    
    NSArray *arrItems = @[self.btnCenter, self.btnLobby, self.btnAtrium, self.btnPassage];
    [self.btnCenter autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.btnCenter autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.btnCenter autoPinEdgeToSuperviewEdge:ALEdgeTop];
    [arrItems autoDistributeViewsAlongAxis:ALAxisVertical alignedTo:ALAttributeLeft withFixedSpacing:0];
    
    [self.btnLobby autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.btnAtrium autoSetDimension:ALDimensionWidth toSize:160];
    [self.btnPassage autoSetDimension:ALDimensionWidth toSize:160];
    
    [self.btnEscalator autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.btnEscalator autoPinEdgeToSuperviewEdge:ALEdgeBottom];
    [self.btnEscalator autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.btnLobby];
    [self.btnEscalator autoSetDimension:ALDimensionWidth toSize:160];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)resetButtonStates:(NSArray *)areaList
{
    BOOL foundId1 = NO;
    BOOL foundId2 = NO;
    BOOL foundId3 = NO;
    BOOL foundId4 = NO;
    BOOL foundId5 = NO;
    for (NSDictionary *dicItem in areaList) {
        if ([dicItem[@"id"] isEqualToString:@"1"]) {
            foundId1 = YES;
        }
        if ([dicItem[@"id"] isEqualToString:@"2"]) {
            foundId2 = YES;
        }
        if ([dicItem[@"id"] isEqualToString:@"3"]) {
            foundId3 = YES;
        }
        if ([dicItem[@"id"] isEqualToString:@"4"]) {
            foundId4 = YES;
        }
        if ([dicItem[@"id"] isEqualToString:@"5"]) {
            foundId5 = YES;
        }
    }
    _btnCenter.enabled = foundId1;
    _btnLobby.enabled = foundId2;
    _btnAtrium.enabled = foundId3;
    _btnPassage.enabled = foundId4;
    _btnEscalator.enabled = foundId5;

}

- (void)requestDatas
{
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryAreaList";
    NSDictionary *dicParam = @{@"taskId":self.taskId?:@""};
    NSOperation *operation = [self.networkMgr operationWithURL:kBaseURL(interface)
                                                                interface:interface
                                                                   params:dicParam
                                                                 dataType:BRNetMgrDataTypeJSON
                                                                   method:BRNetMgrMethodPost];
    [self.networkMgr startOperation:operation];
}

#pragma mark =============  BRNetworkMgr Delegate  =============
- (void)httpOperationSuccess:(AFHTTPRequestOperation *)operation
                      result:(NSDictionary *)result
{
    [self resetButtonStates:result[@"areaList"]];

    if (!self.isShowHistory) {
        
        if ([[DataMgr getInstance].roleId isEqualToString:@"2"]) {
            NSDictionary *dictProgress = result[@"progress"];
            [self.btnUncheck setTitle:[NSString stringWithFormat:@"未上报(%d)", [dictProgress[@"unfinished"] intValue]] forState:UIControlStateNormal];
            [self.btnChecked setTitle:[NSString stringWithFormat:@"已上报(%d)", [dictProgress[@"finished"] intValue]] forState:UIControlStateNormal];
        }else {
            NSDictionary *dictProgress = result[@"progress"];
            [self.btnUncheck setTitle:[NSString stringWithFormat:@"已上报(%d)", [dictProgress[@"unfinished"] intValue]] forState:UIControlStateNormal];
            [self.btnChecked setTitle:[NSString stringWithFormat:@"已检查(%d)", [dictProgress[@"finished"] intValue]] forState:UIControlStateNormal];
        }

    }

}

- (void)httpOperationFailed:(AFHTTPRequestOperation *)operation
                      error:(NSError *)error
{
    
}

- (void)btnUncheckClick:(id)sender
{
    TaskPointViewController *detailVC = [[TaskPointViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.taskId = self.taskId;
    detailVC.pointType = TaskPointTypeUnchecked;
    detailVC.isShowHistory = self.isShowHistory;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)btnCheckedClick:(id)sender
{
    TaskPointViewController *detailVC = [[TaskPointViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.taskId = self.taskId;
    detailVC.pointType = TaskPointTypeChecked;
    detailVC.isShowHistory = self.isShowHistory;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (void)pointItemClick:(id)sender
{
    NSString *areaId = nil;
    if ([sender isEqual:_btnCenter]) {
        areaId = @"1";
    }else if ([sender isEqual:_btnLobby]) {
        areaId = @"2";
    }else if ([sender isEqual:_btnAtrium]) {
        areaId = @"3";
    }else if ([sender isEqual:_btnPassage]) {
        areaId = @"4";
    }else if ([sender isEqual:_btnEscalator]) {
        areaId = @"5";
    }
    
    TaskPointViewController *detailVC = [[TaskPointViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.areaId = areaId;
    detailVC.taskId = self.taskId;
    detailVC.pointType = TaskPointTypeArea;
    detailVC.isShowHistory = self.isShowHistory;
    [self.navigationController pushViewController:detailVC animated:YES];
}

- (UIView *)viewPointItem
{
    if (!_viewPointItem) {
        self.viewPointItem = [UIView newAutoLayoutView];
        [self.view addSubview:_viewPointItem];
    }
    return _viewPointItem;
}


- (UIButton *)btnCenter
{
    if (!_btnCenter) {
        self.btnCenter = [self getAutoLayoutButton];
        [_btnCenter setBackgroundImage:[UIImage imageNamed:@"Task_Point/image_point_center.png"] forState:UIControlStateNormal];
        [_btnCenter addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_btnCenter];
    }
    return _btnCenter;
}

- (UIButton *)btnLobby
{
    if (!_btnLobby) {
        self.btnLobby = [self getAutoLayoutButton];
        [_btnLobby setBackgroundImage:[UIImage imageNamed:@"Task_Point/image_point_lobby.png"] forState:UIControlStateNormal];
        [_btnLobby addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_btnLobby];
    }
    return _btnLobby;
}

- (UIButton *)btnAtrium
{
    if (!_btnAtrium) {
        self.btnAtrium = [self getAutoLayoutButton];
        [_btnAtrium setBackgroundImage:[UIImage imageNamed:@"Task_Point/image_point_atrium.png"] forState:UIControlStateNormal];
        [_btnAtrium addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_btnAtrium];
    }
    return _btnAtrium;
}

- (UIButton *)btnPassage
{
    if (!_btnPassage) {
        self.btnPassage = [self getAutoLayoutButton];
        [_btnPassage setBackgroundImage:[UIImage imageNamed:@"Task_Point/image_point_passage.png"] forState:UIControlStateNormal];
        [_btnPassage addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_btnPassage];
    }
    return _btnPassage;
}

- (UIButton *)btnEscalator
{
    if (!_btnEscalator) {
        self.btnEscalator = [self getAutoLayoutButton];
        [_btnEscalator setBackgroundImage:[UIImage imageNamed:@"Task_Point/image_point_escalator.png"] forState:UIControlStateNormal];
        [_btnEscalator addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_btnEscalator];
    }
    return _btnEscalator;
}

- (UIButton *)getAutoLayoutButton
{
    UIButton *btnTemp = [UIButton buttonWithType:UIButtonTypeCustom];
    btnTemp.translatesAutoresizingMaskIntoConstraints = NO;
    btnTemp.backgroundColor = [UIColor clearColor];
    [btnTemp addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
    return btnTemp;
}

/*

- (TaskPointItem *)itemCenter
{
    if (!_itemCenter) {
        self.itemCenter = [TaskPointItem newAutoLayoutView];
        _itemCenter.namePosition = TaskItemPositionTop;
        [_itemCenter setNameBackgroundColor:RGBColor(37, 124, 201, 0.5)];
        [_itemCenter addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_itemCenter];
    }
    return _itemCenter;
}

- (TaskPointItem *)itemLobby
{
    if (!_itemLobby) {
        self.itemLobby = [TaskPointItem newAutoLayoutView];
        _itemLobby.namePosition = TaskItemPositionTop;
        [_itemLobby setNameBackgroundColor:RGBColor(158, 131, 76, 0.5)];
        [_itemLobby addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_itemLobby];
    }
    return _itemLobby;
}

- (TaskPointItem *)itemAtrium
{
    if (!_itemAtrium) {
        self.itemAtrium = [TaskPointItem newAutoLayoutView];
        _itemAtrium.namePosition = TaskItemPositionBottom;
        [_itemAtrium setNameBackgroundColor:RGBColor(105, 156, 175, 0.5)];
        [_itemAtrium addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_itemAtrium];
    }
    return _itemAtrium;
}

- (TaskPointItem *)itemPassage
{
    if (!_itemPassage) {
        self.itemPassage = [TaskPointItem newAutoLayoutView];
        _itemPassage.namePosition = TaskItemPositionBottom;
        [_itemPassage setNameBackgroundColor:RGBColor(125, 105, 58, 0.5)];
        [_itemPassage addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_itemPassage];
    }
    return _itemPassage;
}

- (TaskPointItem *)itemEscalator
{
    if (!_itemEscalator) {
        self.itemEscalator = [TaskPointItem newAutoLayoutView];
        _itemEscalator.namePosition = TaskItemPositionBottom;
        [_itemEscalator setNameBackgroundColor:RGBColor(135, 111, 87, 0.5)];
        [_itemEscalator addTarget:self action:@selector(pointItemClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.viewPointItem addSubview:_itemEscalator];
    }
    return _itemEscalator;
}
*/

- (UIButton *)btnUncheck
{
    if (!_btnUncheck) {
        self.btnUncheck = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnUncheck.translatesAutoresizingMaskIntoConstraints = NO;
        _btnUncheck.backgroundColor = [UIColor clearColor];
        _btnUncheck.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 70);
        _btnUncheck.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_btnUncheck setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnUncheck setBackgroundImage:[UIImage imageNamed:@"Task_Point/btn_point_blue.png"] forState:UIControlStateNormal];
        [_btnUncheck addTarget:self action:@selector(btnUncheckClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *imageVArrow = [UIImageView newAutoLayoutView];
        imageVArrow.image = [UIImage imageNamed:@"Task_Point/image_point_arrow.png"];
        [_btnUncheck addSubview:imageVArrow];
        
        [imageVArrow autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:8];
        [imageVArrow autoSetDimensionsToSize:CGSizeMake(13, 13)];
        [imageVArrow autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
        [self.view addSubview:_btnUncheck];
    }
    return _btnUncheck;
}

- (UIButton *)btnChecked
{
    if (!_btnChecked) {
        self.btnChecked = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnChecked.translatesAutoresizingMaskIntoConstraints = NO;
        _btnChecked.backgroundColor = [UIColor clearColor];
        _btnChecked.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 70);
        _btnChecked.titleLabel.font = [UIFont boldSystemFontOfSize:16];
        [_btnChecked setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnChecked setBackgroundImage:[UIImage imageNamed:@"Task_Point/btn_point_blue.png"] forState:UIControlStateNormal];
        [_btnChecked addTarget:self action:@selector(btnCheckedClick:) forControlEvents:UIControlEventTouchUpInside];
        
        UIImageView *imageVArrow = [UIImageView newAutoLayoutView];
        imageVArrow.image = [UIImage imageNamed:@"Task_Point/image_point_arrow.png"];
        [_btnChecked addSubview:imageVArrow];
        
        [imageVArrow autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:8];
        [imageVArrow autoSetDimensionsToSize:CGSizeMake(13, 13)];
        [imageVArrow autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
        [self.view addSubview:_btnChecked];
    }
    return _btnChecked;
}

@end

@implementation TaskPointItem

- (void)dealloc
{
    self.lblName = nil;
    self.viewName = nil;
}

- (void)layoutSubviews
{
    self.backgroundColor = [UIColor lightGrayColor];
    
    self.layer.borderWidth = 1;
    self.layer.borderColor = [UIColor whiteColor].CGColor;
    
    if (self.namePosition == TaskItemPositionTop) {
        [self.viewName autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
        [self.viewName autoSetDimension:ALDimensionHeight toSize:30];
        self.lblName.textAlignment = NSTextAlignmentLeft;
    }else {
        [self.viewName autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeTop];
        [self.viewName autoSetDimension:ALDimensionHeight toSize:50];
        self.lblName.textAlignment = NSTextAlignmentCenter;
        self.lblName.numberOfLines = 0;
    }
    [self.lblName autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
    
    [super layoutSubviews];
}

- (void)setNameBackgroundColor:(UIColor *)color
{
    UIView *viewBG = [self.viewName viewWithTag:100];
    [viewBG setBackgroundColor:color];
}

- (UIView *)viewName
{
    if (!_viewName) {
        self.viewName = [UIView newAutoLayoutView];
        UIView *viewBG = [UIView newAutoLayoutView];
        viewBG.tag = 100;
        [_viewName addSubview:viewBG];
        [viewBG autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0)];
        [self addSubview:_viewName];
    }
    return _viewName;
}

- (UILabel *)lblName
{
    if (!_lblName) {
        self.lblName = [UILabel newAutoLayoutView];
        _lblName.font = [UIFont boldSystemFontOfSize:20];
        _lblName.textColor = [UIColor whiteColor];
        [self.viewName addSubview:self.lblName];
    }
    return _lblName;
}

@end

