//
// UniversalViewWrapper.m
// Project
//
// Created by Ziv on 16/11/15.
//
//

#import "UniversalViewWrapper.h"
#import "Lang.h"
#import "SDK.h"
#import <PureLayout/PureLayout.h>

@implementation UniversalViewWrapper

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

- (id)initWithCoder:(NSCoder *)decoder {
	self = [super initWithCoder:decoder];
	if (self) {
	}
	return self;
}

#pragma mark - Property

- (void)setViewClassName:(NSString *)viewClassName {
	if (_viewClassName != viewClassName) {
		_viewClassName = viewClassName;
		[self loadViewByClassName];
	}
}

- (void)setViewNibName:(NSString *)viewNibName {
	if (_viewNibName != viewNibName) {
		_viewNibName = viewNibName;
		[self loadViewByNibName];
	}
}

#pragma mark - Public

- (__kindof UIView *)get {
	return [self viewWithTag:NSStringFromClass([UIView class]).hash];
}

#pragma mark - Private

- (void)loadViewByClassName {
	if (!self.viewClassName) {
		return;
	}
	[[self get] removeFromSuperview];
	UIView *view = [[NSClassFromString(self.viewClassName) alloc] init];
	view.tag = NSStringFromClass([UIView class]).hash;
	[self addSubview:view];
	[view autoPinEdgesToSuperviewEdges];
}

- (void)loadViewByNibName {
	if (!self.viewNibName) {
		return;
	}
	[[self get] removeFromSuperview];
	UIView *view = [[NSBundle mainBundle] loadNibNamed:self.viewNibName owner:nil options:nil].firstObject;
	view.tag = NSStringFromClass([UIView class]).hash;
	[view awakeFromNib];
	[self addSubview:view];
	[view autoPinEdgesToSuperviewEdges];
}

@end
