//
//  SLHomeTopCollectionViewCell.m
//  SLAiELTS
//
//  Created by Gusont on 2023/3/10.
//

#import "SLHomeTopCollectionViewCell.h"
#import "SLMessageViewController.h"
#import "SLTargetViewController.h"

@implementation SLHomeTopCollectionViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    [self configContentView];
}

- (void)drawRect:(CGRect)rect {
    [self.hintBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:4];
    [self.lxBtn layoutButtonWithEdgeInsetsStyle:MKButtonEdgeInsetsStyleRight imageTitleSpace:3];
}

- (void)configContentView {
    UIImage *image = [UIImage gradientImageSize:CGSizeMake(kSCREEN_WIDTH, 243 * KScaleW) startPoint:CGPointMake(0, 0) endPoint:CGPointMake(0, 243 * KScaleW) colors:@[SLColor(@"#B2E8FF"), [SLColor(@"#B2E8FF") colorWithAlphaComponent:0.53], [SLColor(@"#B2E8FF") colorWithAlphaComponent:0.05]]];
    self.topBgImageView.image = image;
    
    UIImage *image1 = [UIImage gradientImageSize:CGSizeMake(kSCREEN_WIDTH - 40, 170 * KScaleW) startPoint:CGPointMake(0, 0) endPoint:CGPointMake(kSCREEN_WIDTH - 40, 0 ) colors:@[SLColor(@"#D3F7FF"), SLColor(@"#AFCFFF")] cornerRadius:0];
    self.btmBgView.image = image1;
    self.btmBgView.layer.cornerRadius = 20.f;
    self.hintBtn.backgroundColor = [SLColor(@"#080F1B") colorWithAlphaComponent:0.1];
    CGSize hintBtnSize = [self.hintBtn sizeThatFits:CGSizeMake(MAXFLOAT, 30)];
    [self.hintBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(hintBtnSize.width + 24);
    }];
    CGSize lxBtnSize = [self.lxBtn sizeThatFits:CGSizeMake(MAXFLOAT, 30)];
    [self.lxBtn mas_updateConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(lxBtnSize.width + 24);
    }];
    self.hintBtn.layer.cornerRadius = 15.0f;
    self.lxBtn.layer.cornerRadius = 15.0f;
    WS(weakSelf);
    [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"targetModel" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        SLTargetModel *targetModel = [SLGlobalInfo SharedInstance].targetModel;
        if (targetModel) {
            
            if ([targetModel getGoalTimeInterval] > 0) {
                [weakSelf.hintBtn setTitle:[NSString stringWithFormat:@"距离考%@分还有%ld天",targetModel.goalFraction,[targetModel getGoalTimeInterval]] forState:UIControlStateNormal];
            }else {
                [weakSelf.hintBtn setTitle:[NSString stringWithFormat:@"距离考-分还有--天"] forState:UIControlStateNormal];
            }
            
            CGSize hintBtnSize = [weakSelf.hintBtn sizeThatFits:CGSizeMake(MAXFLOAT, 30)];
            [weakSelf.hintBtn mas_updateConstraints:^(MASConstraintMaker *make) {
                make.width.mas_equalTo(hintBtnSize.width + 24);
            }];

        }
        [weakSelf setNeedsDisplay];
    }];
    [self.KVOController observe:[SLGlobalInfo SharedInstance] keyPath:@"loginInfo" options:NSKeyValueObservingOptionNew | NSKeyValueObservingOptionOld block:^(id  _Nullable observer, id  _Nonnull object, NSDictionary<NSKeyValueChangeKey,id> * _Nonnull change) {
        SLLoginInfo *loginInfo = [SLGlobalInfo SharedInstance].loginInfo;
        if (loginInfo) {
            NSString *headUrl = [NSString stringWithFormat:@"%@%@",[SLHttpCenter SharedInstance].serverUrl, [SLGlobalInfo SharedInstance].loginInfo.user.userHead];
            [weakSelf.headImageView sd_setImageWithURL:[NSURL URLWithString:headUrl] placeholderImage:ImageName(@"icon_ellipse")];
        }else {
           
            weakSelf.headImageView .image = ImageName(@"icon_ellipse");
        }
    }];
}

- (void)confignCellWithModel:(SLPageModel *)pageModel {
    
}

- (IBAction)lxBtnClick:(id)sender {
    SLMessageViewController *vc = [SLMessageViewController loadViewControllewWithNib];
    [self.viewController navPushViewController:vc animated:YES];
}

- (IBAction)hitBtnClick:(id)sender {
    SLTargetViewController *vc = [SLTargetViewController loadViewControllewWithNib];
    [self.viewController navPushViewController:vc animated:YES];
}

@end
