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

#import "TaskHistoryViewController.h"
#import "TaskHistoryInfosViewController.h"
#import "ImageBrowserViewController.h"
#import "TaskAreaViewController.h"
#import <RDVTabBarController.h>
#import "BRNetworkMgr.h"

@interface TaskHistoryViewController () < ImageBrowserDelegate, BRNetworkMgrDelegate >

@property (nonatomic, strong) TaskHistoryInfosViewController *vcTaskHisotyInfos;
@property (nonatomic, strong) ImageBrowserViewController *vcImageBrowser;
@property (nonatomic, strong) UIButton *btnInspectScore;
@property (strong, nonatomic) UILabel *lblRemarkPre;
@property (strong, nonatomic) UILabel *lblRemark;
@property (strong, nonatomic) UIView *viewRemark;

@property (strong, nonatomic) UIScrollView *scrollVContainer;

@end

@implementation TaskHistoryViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title = @"历史任务";
        
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.vcTaskHisotyInfos = [[TaskHistoryInfosViewController alloc] initWithNibName:@"TaskHistoryInfosViewController" bundle:nil];
    [self.view addSubview:_vcTaskHisotyInfos.view];
    
    [_vcTaskHisotyInfos.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 0, 0, 0) excludingEdge:ALEdgeBottom];
    [_vcTaskHisotyInfos.view autoSetDimension:ALDimensionHeight toSize:177];
    
    UIView *viewLine = [UIView newAutoLayoutView];
    viewLine.backgroundColor = [UIColor whiteColor];
    [viewLine setFrame:CGRectMake(0, 176.5, 320, 0.5)];
    [_vcTaskHisotyInfos.view addSubview:viewLine];
    
    self.viewRemark = [UIView newAutoLayoutView];
    _viewRemark.backgroundColor = [UIColor colorWithRed:0.2118 green:0.7098 blue:0.9490 alpha:1];
    [self.view addSubview:_viewRemark];
    [_viewRemark addSubview:self.lblRemarkPre];
    [_viewRemark addSubview:self.lblRemark];
    
//    [self.vcImageBrowser.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.viewRemark withOffset:0];
//    [self.vcImageBrowser.view autoPinEdgeToSuperviewEdge:ALEdgeLeft];
//    [self.vcImageBrowser.view autoPinEdgeToSuperviewEdge:ALEdgeRight];
    
//    [self.btnInspectScore autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.vcImageBrowser.view withOffset:30 relation:NSLayoutRelationLessThanOrEqual];
//    [self.btnInspectScore autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.vcImageBrowser.view withOffset:5 relation:NSLayoutRelationGreaterThanOrEqual];
    
    [self setViewLayoutConstraint];
    
    self.lblRemark.text = @"艾萨克·谢尔比是肯塔基州的第一和第五任州长，并担任过弗吉尼亚州和北卡罗莱纳州的州议员。他还曾参与邓莫尔勋爵之战";
    
    [self requestDatas];
}

- (void)setViewLayoutConstraint
{
    [self.lblRemarkPre autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(4, 8, 0, 8) excludingEdge:ALEdgeBottom];
    [self.lblRemarkPre autoSetDimension:ALDimensionHeight toSize:21];
    
    [self.lblRemark autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:8];
    [self.lblRemark autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblRemarkPre];
    [self.lblRemark autoSetDimension:ALDimensionWidth toSize:304];
    [self.lblRemark sizeToFit];
    
    [self.viewRemark autoPinEdgeToSuperviewEdge:ALEdgeLeft];
    [self.viewRemark autoPinEdgeToSuperviewEdge:ALEdgeRight];
    [self.viewRemark autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.vcTaskHisotyInfos.view];
    [self.btnInspectScore autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.viewRemark withOffset:40];
    [self.btnInspectScore autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:20];
    [self.btnInspectScore autoSetDimensionsToSize:CGSizeMake(280, 30)];
    [self.btnInspectScore autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:20];

}

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

- (void)requestDatas
{
    [UIApplication showLoadingView:kTextShowInProgress];
    NSString *interface = @"queryTaskDetails";
    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
{
    NSDictionary *dictTaskDetail = result[@"taskDetails"];
    _vcTaskHisotyInfos.lblStartDate.text = dictTaskDetail[@"startDate"];;
    _vcTaskHisotyInfos.lblEndDate.text = dictTaskDetail[@"endDate"];
    _vcTaskHisotyInfos.lblName.text = dictTaskDetail[@"activityName"];
    _vcTaskHisotyInfos.lblAddress.text = dictTaskDetail[@"shopName"];
    _vcTaskHisotyInfos.lblType.text = @"企划部-促销";
    _vcTaskHisotyInfos.lblName.numberOfLines=0;
    
    _vcTaskHisotyInfos.lblScore.text = [NSString stringWithFormat:@"%d", [result[@"sumScore"] intValue]];
    self.lblRemark.text = dictTaskDetail[@"details"];
}

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

- (void)btnInspectScoreClick:(id)sender
{
    TaskAreaViewController *detailVC = [[TaskAreaViewController alloc] initWithNibName:nil bundle:nil];
    detailVC.isShowHistory = YES;
    detailVC.taskId = self.taskId;
    detailVC.activityName = self.activityName;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark =============  ImageBrowserViewController Delegate  =============

- (void)imageBrowser:(ImageBrowserViewController *)imageBrowser didSelectImages:(NSArray *)images
{
    
}
- (ImageBrowserViewController *)vcImageBrowser
{
    if (!_vcImageBrowser) {
        self.vcImageBrowser = [[ImageBrowserViewController alloc] initWithNibName:nil bundle:nil];
        _vcImageBrowser.delegate = self;
        _vcImageBrowser.showAddPicsButton = NO;
        _vcImageBrowser.vcImagePickerPrensentFrom = self.navigationController;
        [self addChildViewController:_vcImageBrowser];
        [self.view addSubview:_vcImageBrowser.view];
    }
    return _vcImageBrowser;
}

- (TaskHistoryInfosViewController *)vcTaskHisotyInfos
{
    if (!_vcTaskHisotyInfos) {
        self.vcTaskHisotyInfos = [[TaskHistoryInfosViewController alloc] initWithNibName:@"TaskHistoryInfosViewController" bundle:nil];
        _vcTaskHisotyInfos.view.frame = CGRectMake(0, 0, CGRectGetWidth(_vcTaskHisotyInfos.view.frame), CGRectGetHeight(_vcTaskHisotyInfos.view.frame));
        [self.view addSubview:_vcTaskHisotyInfos.view];
    }
    return _vcTaskHisotyInfos;
}

- (UIButton *)btnInspectScore
{
    if (!_btnInspectScore) {
        self.btnInspectScore = [UIButton buttonWithType:UIButtonTypeCustom];
        _btnInspectScore.translatesAutoresizingMaskIntoConstraints = NO;
        _btnInspectScore.backgroundColor = [UIColor colorWithRed:0.2118 green:0.7098 blue:0.9490 alpha:1];
        _btnInspectScore.layer.cornerRadius = 3;
        [_btnInspectScore setTitle:@"评分查看" forState:UIControlStateNormal];
        [_btnInspectScore setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_btnInspectScore setBackgroundImage:[UIImage imageNamed:@"btn_login.png"] forState:UIControlStateNormal];
        [_btnInspectScore addTarget:self action:@selector(btnInspectScoreClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_btnInspectScore];
    }
    return _btnInspectScore;
}

- (UILabel *)lblRemarkPre
{
    if (!_lblRemarkPre) {
        self.lblRemarkPre = [UILabel newAutoLayoutView];
        _lblRemarkPre.text = @"任务描述及要求：";
        _lblRemarkPre.textColor = kCommonTextPrefixColor;
        _lblRemarkPre.backgroundColor = [UIColor clearColor];
        _lblRemarkPre.textAlignment = NSTextAlignmentLeft;
        _lblRemarkPre.font = [UIFont systemFontOfSize:16];
    }
    return _lblRemarkPre;
}

- (UILabel *)lblRemark
{
    if (!_lblRemark) {
        self.lblRemark = [UILabel newAutoLayoutView];
        _lblRemark.textColor = [UIColor whiteColor];
        _lblRemark.backgroundColor = [UIColor clearColor];
        _lblRemark.textAlignment = NSTextAlignmentLeft;
        _lblRemark.font = [UIFont systemFontOfSize:15];
        _lblRemark.numberOfLines = 0;
    }
    return _lblRemark;
}

@end
