//
// HistoryViewController.m
// Project
//
// Created by Ziv on 16/1/19.
//
//

#import "HistoryViewController.h"
#import "SDK.h"
#import "UIConstants.h"
#import "UIToolkit.h"

@interface HistoryViewController ()
@property (nonatomic, strong) NSArray *cellDataArray;
@end

@implementation HistoryViewController

- (instancetype)init {
	if (self = [super init]) {
		self.title = @"History";
	}
	return self;
}

- (void)viewDidLoad {
	[super viewDidLoad];

	self.tableView.estimatedRowHeight = 500;
	if (YES) {
		UIImageView *historyImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"bg_history"]];
		self.tableView.tableHeaderView = historyImageView;
	}
	[self builCellDataSource];
}

#pragma mark - Private

- (void)builCellDataSource {
	NSMutableArray *array = [NSMutableArray array];
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"history" ofType:@"plist"];
	NSArray *dataSource = [NSArray arrayWithContentsOfFile:plistPath];
	for (NSDictionary *dict in dataSource) {
		TableViewCellData *data = [[TableViewCellData alloc] init];
		NSMutableAttributedString *historyText = [[NSMutableAttributedString alloc] init];
		[historyText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:5] }]];
		[historyText appendAttributedString:[[NSAttributedString alloc] im_initWithString:dict[@"Title"] attributes:@{ NSForegroundColorAttributeName : [UIColor midnightColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleBody] }]];
		[historyText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n\n" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:5] }]];
		[historyText appendAttributedString:[[NSAttributedString alloc] im_initWithString:dict[@"Content"] attributes:@{ NSForegroundColorAttributeName : [UIColor lightGrayColor], NSFontAttributeName : [UIFont preferredFontForTextStyle:UIFontTextStyleFootnote] }]];
		[historyText appendAttributedString:[[NSAttributedString alloc] im_initWithString:@"\n" attributes:@{ NSFontAttributeName : [UIFont systemFontOfSize:5] }]];
		data.attributedText = [[NSAttributedString alloc] initWithAttributedString:historyText];
		data.allowsTextLineWrapping = YES;
		[array addObject:data];
	}
	self.cellDataArray = [NSArray arrayWithArray:array];
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
	return 1;
}

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

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	return [UIToolkit tableView:tableView atIndexPath:indexPath cellWithData:data];
}

#pragma mark - Table view delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	TableViewCellData *data = self.cellDataArray[indexPath.row];
	return [UIToolkit tableView:tableView atIndexPath:indexPath rowHeightWithData:data];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
