//
// DateSelectionView.m
// Project
//
// Created by Ziv on 16/8/3.
//
//

#import "CustomLeftRightTitleToolbar.h"
#import "Lang.h"
#import "SDK.h"
#import "DrawUtils.h"
#import <libextobjc/EXTScope.h>
#import "Utility.h"
#import "Vars.h"

@interface CustomLeftRightTitleToolbar ()
@property (nonatomic, copy) void (^leftBarButtonItemHandler) (void);
@property (nonatomic, copy) void (^rightBarButtonItemHandler) (void);
@property (nonatomic, copy) void (^titleBarButtonItemHandler) (void);
@end

@implementation CustomLeftRightTitleToolbar

- (instancetype)initWithFrame:(CGRect)frame {
	if (self = [super initWithFrame:frame]) {
		[self loadView];
	}
	return self;
}

- (instancetype)initWithCoder:(NSCoder *)aDecoder {
	if (self = [super initWithCoder:aDecoder]) {
		[self loadView];
	}
	return self;
}

#pragma mark - Private

- (void)loadView {
	@weakify(self)
	self.backgroundColor = [UIColor whiteColor];
	self.leftBarButtonItem = [[UIBarButtonItem alloc] im_initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome angleLeftIconWithSize:32] fillColor:[UIColor blackColor]] style:UIBarButtonItemStylePlain handler:^(id sender) {
		@strongify(self)
		if (self.leftBarButtonItemHandler) {
			self.leftBarButtonItemHandler();
		}
	}];
	self.rightBarButtonItem = [[UIBarButtonItem alloc] im_initWithImage:[DrawUtils imageWithFAKIcon:[FAKFontAwesome angleRightIconWithSize:32] fillColor:[UIColor blackColor]] style:UIBarButtonItemStylePlain handler:^(id sender) {
		@strongify(self)
		if (self.rightBarButtonItemHandler) {
			self.rightBarButtonItemHandler();
		}
	}];
	self.titleBarButtonItem = [[UIBarButtonItem alloc] im_initWithTitle:@"" style:UIBarButtonItemStylePlain handler:^(id sender) {
		@strongify(self)
		if (self.titleBarButtonItemHandler) {
			self.titleBarButtonItemHandler();
		}
	}];
	UIBarButtonItem *flexible = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
	[self setItems:@[self.leftBarButtonItem, flexible, self.titleBarButtonItem, flexible, self.rightBarButtonItem] animated:YES];
}

- (void)drawRect:(CGRect)rect {
	// avoid translucent
}

- (void)tintColorDidChange {
	[super tintColorDidChange];

	[self.titleBarButtonItem setTitleTextAttributes:@{NSForegroundColorAttributeName : [Vars defaultTintColor]} forState:UIControlStateNormal];
}

@end
