//
//  SLLxTopView.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/13.
//

#import "SLLxTopView.h"
#import "SLTargetViewController.h"

@implementation SLLxTopView

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

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

- (void)addContentView {
    [[NSBundle mainBundle] loadNibNamed:@"SLLxTopView" owner:self options:nil];
    self.contentView.frame = self.bounds;
    [self addSubview:self.contentView];
    self.contentView.backgroundColor = [UIColor clearColor];
    WS(weakSelf);
    [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"targetModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        [weakSelf setTargetLabelText];
    }];
    [self setTargetLabelText];
        
}

- (void)setTargetLabelText {
    SLTargetModel *targetModel = [SLGlobalInfo SharedInstance].targetModel;
    if (targetModel && [targetModel getGoalTimeInterval] > 0) {//2008年发生了什么大事
        self.timeLabel.attributedText = [NSString getShowAttributedStringWithNormalString:[NSString stringWithFormat:@"%ld",[targetModel getGoalTimeInterval]] normalColor:SLColor(@"#233C61") specialString:@"天" specialColor:SLColor(@"#233C61") specialFont:[UIFont boldSystemFontOfSize:14]];
        self.scoreLabel.attributedText = [NSString getShowAttributedStringWithNormalString:targetModel.goalFraction normalColor:SLColor(@"#233C61") specialString:@"分" specialColor:SLColor(@"#233C61") specialFont:[UIFont boldSystemFontOfSize:14]];
    }else {
        self.timeLabel.attributedText = [NSString getShowAttributedStringWithNormalString:@"--" normalColor:SLColor(@"#233C61") specialString:@"天" specialColor:SLColor(@"#233C61") specialFont:[UIFont boldSystemFontOfSize:14]];
        self.scoreLabel.attributedText = [NSString getShowAttributedStringWithNormalString:@"--" normalColor:SLColor(@"#233C61") specialString:@"分" specialColor:SLColor(@"#233C61") specialFont:[UIFont boldSystemFontOfSize:14]];
    }
}

- (void)drawRect:(CGRect)rect {
    [self.targetBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:0];
    [self.distanceTestBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:0];
}
- (IBAction)pushTargetVc:(id)sender {
    SLTargetViewController *vc = [SLTargetViewController loadViewControllewWithNib];
    [self.viewController navPushViewController:vc animated:YES];
}

@end
