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

#import "BackGroundMusicViewController.h"
#import "BRNetworkMgr.h"
#import "RDVTabBarController.h"
#import "DBDaoMgr.h"

@interface BackGroundMusicViewController () < BRNetworkMgrDelegate >
{
    UILabel*lblNotice;
    UIImageView* imageNotice;
    UILabel*lblRemind;
    UIImageView* imageRemind;
    UIImageView* noImage;
}
@property(nonatomic,strong)UIButton* btnLeft;
@property(nonatomic,strong)UIButton* btnRight;
@property(nonatomic,strong)NSArray * noticeArr;

@property(nonatomic,strong)NSString * tableType;;

@end

@implementation BackGroundMusicViewController

- (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.
    UIButton *btnBack = [UIButton buttonWithType:UIButtonTypeCustom];
    [btnBack setBackgroundImage:[UIImage imageNamed:@"NavBar/btn_nav_back.png"] forState:UIControlStateNormal];
    [btnBack sizeToFit];
    [btnBack addTarget:self action:@selector(btnBackClick:) forControlEvents:UIControlEventTouchUpInside];
    
    [self.navigationController navbarLeftItemCustomized:@[btnBack] onwer:self.navigationItem];

    self.tableVContent.separatorStyle = UITableViewCellSelectionStyleNone;
    
    noImage = [[UIImageView alloc] init];
    noImage.image = [UIImage imageNamed:@"pic_zanwushuju.png"];
    [self.view addSubview:noImage];
    [noImage autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
    [noImage autoAlignAxisToSuperviewAxis:ALAxisVertical];
    [noImage autoSetDimensionsToSize:CGSizeMake(170, 170)];

    [self selectNoticeDatas];
}

- (void)setViewLayoutConstraint
{
    [self.tableVContent autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(10, 0, 0, 0)];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    self.rdv_tabBarController.navigationController.navigationBarHidden = YES;
    [self setViewLayoutConstraint];
}
-(void)selectNoticeDatas
{
    _tableType = @"notice";
    _noticeArr = [DBDaoMgr fetchBackGroundMusic:[DataMgr getInstance].userID];
    [self.tableVContent reloadData];
    if (_noticeArr.count!=0) {
//        self.imageVNoResult.hidden = YES;
        noImage.hidden = YES;
    }
    else
    {
//        self.imageVNoResult.hidden = NO;
//        [self.imageVNoResult autoAlignAxisToSuperviewAxis:ALAxisHorizontal];
//        [self.imageVNoResult autoAlignAxisToSuperviewAxis:ALAxisVertical];
//        [self.imageVNoResult autoSetDimensionsToSize:CGSizeMake(150, 150)];
        noImage.hidden = NO;
    }
    
}
- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *cellIdentifier = @"NoticeListCell";
    BackGroundMusicListCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[BackGroundMusicListCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
    }
    cell.lblTitle.text = _noticeArr[indexPath.row][@"title"];
    cell.lblcontent.text = _noticeArr[indexPath.row][@"content"];
//    cell.lblcontent.text = @"深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了";
    cell.lblCreateTime.text = _noticeArr[indexPath.row][@"time"];
    
    if ([_noticeArr[indexPath.row][@"isRead"] isEqualToString:@"NO"]) {
        cell.lblTitle.textColor = [UIColor darkGrayColor];
        cell.lblCreateTime.textColor = [UIColor darkGrayColor];
        cell.lblcontent.textColor = [UIColor darkGrayColor];
    }else if ([_noticeArr[indexPath.row][@"isRead"] isEqualToString:@"YES"]) {
        cell.lblTitle.textColor = [UIColor lightGrayColor];
        cell.lblCreateTime.textColor = [UIColor lightGrayColor];
        cell.lblcontent.textColor = [UIColor lightGrayColor];
    }
    
    return cell;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *strTitle = _noticeArr[indexPath.row][@"context"];
//    NSString *strTitle = @"深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了深按键都ihoaisjdo11阿里开始觉得洛杉矶的礼物；路上看到了";
    return 55+5+5+[strTitle sizeWithFont:[UIFont systemFontOfSize:13]
                       constrainedToSize:CGSizeMake(220, 190) lineBreakMode:NSLineBreakByTruncatingTail].height;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.noticeArr.count;
}

#pragma mark =============  UITableView Delegate  =============

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedIndexPath = indexPath;
    
    NSString* nid = _noticeArr[indexPath.row][@"noticeId"];
    [DBDaoMgr updateNoticeById:nid];
    [self selectNoticeDatas];
    [[NSNotificationCenter defaultCenter] postNotificationName:kShouldReQueryCountInit object:nil];
    
}

@end

@implementation BackGroundMusicListCell

- (void)dealloc
{
    self.lblTitle = nil;
    self.lblCreateTime = nil;
    self.viewLine = nil;
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        [self.lblCreateTime autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
        [self.lblCreateTime autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:10];
        [self.lblCreateTime autoSetDimensionsToSize:CGSizeMake(80, 20)];
        
        [self.lblTitle autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:5];
        [self.lblTitle autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
        [self.lblTitle autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.lblCreateTime withOffset:10];
        
        [self.lblcontent autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:10];
        [self.lblcontent autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.lblCreateTime];
        [self.lblcontent autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblCreateTime withOffset:10];
        
        [self.viewLine autoSetDimension:ALDimensionHeight toSize:0.5];
        //        [self.viewLine autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.lblTitle withOffset:5];
        [self.viewLine autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsMake(0, 10, 0, 10) excludingEdge:ALEdgeTop];
    }
    return self;
}

//- (void)setSelected:(BOOL)selected animated:(BOOL)animated
//{
//    [super setSelected:selected animated:animated];
//    if (selected) {
//        if (self.tag == 101) {//未读
//            self.lblCreateTime.backgroundColor = RGBColor(229, 0, 129, 1);
//        }else {
//            self.lblCreateTime.backgroundColor = [UIColor grayColor];
//        }
//
//    }
//
//}
//
//- (void)setHighlighted:(BOOL)highlighted animated:(BOOL)animated
//{
//    [super setHighlighted:highlighted animated:animated];
//    if (highlighted) {
//        if (self.tag == 101) {//未读
//            self.lblCreateTime.backgroundColor = RGBColor(229, 0, 129, 1);
//        }else {
//            self.lblCreateTime.backgroundColor = [UIColor grayColor];
//        }
//    }
//
//}

#pragma mark =============  LazyLoad Properties  =============

- (UIView *)viewLine
{
    if (!_viewLine) {
        self.viewLine = [UIView newAutoLayoutView];
        _viewLine.backgroundColor = [UIColor lightGrayColor];
        [self.contentView addSubview:_viewLine];
    }
    return _viewLine;
}
- (UILabel *)lblcontent
{
    if (!_lblcontent) {
        self.lblcontent = [UILabel newAutoLayoutView];
        self.lblcontent.textAlignment = NSTextAlignmentLeft;
        self.lblcontent.backgroundColor = [UIColor clearColor];
        self.lblcontent.textColor = [UIColor blackColor];
        self.lblcontent.font = [UIFont systemFontOfSize:13];
        self.lblcontent.numberOfLines = 0;
        [self.contentView addSubview:_lblcontent];
    }
    return _lblcontent;
}

- (UILabel *)lblTitle
{
    if (!_lblTitle) {
        self.lblTitle = [UILabel newAutoLayoutView];
        self.lblTitle.textAlignment = NSTextAlignmentLeft;
        self.lblTitle.backgroundColor = [UIColor clearColor];
        self.lblTitle.textColor = [UIColor blackColor];
        self.lblTitle.font = [UIFont boldSystemFontOfSize:15];
        self.lblTitle.numberOfLines = 0;
        [self.contentView addSubview:_lblTitle];
    }
    return _lblTitle;
}

- (UILabel *)lblCreateTime
{
    if (!_lblCreateTime) {
        self.lblCreateTime = [UILabel newAutoLayoutView];
        self.lblCreateTime.textColor = [UIColor whiteColor];
        self.lblCreateTime.textAlignment = NSTextAlignmentCenter;
        self.lblCreateTime.font = [UIFont boldSystemFontOfSize:14];
        [self.contentView addSubview:_lblCreateTime];
    }
    return _lblCreateTime;
}

@end

