//
//  GuideView.m
//  创维盒子
//
//  Created by APPLE on 2023/9/18.
//

#import "GuideView.h"
#import <Masonry.h>
#import "DDYLanguageTool.h"

@interface GuideView(){
    UIView *pointSelectView;
}

@end

@implementation GuideView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

- (id)initWithTips:(NSString *)tips imageName:(NSString *)imageName index:(NSInteger)index{
    self = [super init];
    
    [self drawAnyViewWithTips:tips imageName:imageName index:index];
    
    return self;
}

- (void)drawAnyViewWithTips:(NSString *)tips imageName:(NSString *)imageName index:(NSInteger)index{
    [self setBackgroundColor:HWECECECColor];
    
    /*背景视图*/
    UIImageView *bgImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide_bg_icon"]];
    [self addSubview:bgImageView];
    [bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
//    guide_up_icon
    UIImageView *upImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"guide_up_icon"]];
    [bgImageView addSubview:upImageView];
    [upImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(H_STATE_BAR + 12.f);
        make.left.mas_equalTo(25.f);
        make.width.mas_equalTo(132);
        make.height.mas_equalTo(28);
    }];
    
    /*跳过*/
    bgImageView.userInteractionEnabled = YES;
    UIButton *skipBtn = [[UIButton alloc] init];
    [skipBtn addTarget:self action:@selector(didClickButFun:) forControlEvents:(UIControlEventTouchUpInside)];
    [skipBtn setTitle:NSLocalizedString(@"guide_skip",nil) forState:(UIControlStateNormal)];
    //[skipBtn setTitleColor:HW999999Color forState:(UIControlStateNormal)];
    [skipBtn.titleLabel setFont:[UIFont systemFontOfSize:14.f]];
    [skipBtn.layer setCornerRadius:14.f];
    skipBtn.layer.borderColor = [UIColor whiteColor].CGColor;
    skipBtn.layer.borderWidth = 1.0;
    //[skipBtn setBackgroundColor:HWE3E8F1Color];
    skipBtn.clipsToBounds = YES;
    [self addSubview:skipBtn];
    [skipBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20);
        make.width.mas_equalTo(80.f);
        make.top.equalTo(upImageView.mas_top).offset(0.f);
        make.height.mas_equalTo(28.f);
    }];
    
    NSString* curLanguage = [DDYLanguageTool ddy_AppLanguage];
    if(!curLanguage || curLanguage.length == 0){
        curLanguage = [DDYLanguageTool ddy_SystemLanguage];
    }
    
    CGFloat curFontSize = 30.0;
    // "zh-Hant-HK", "zh-Hans-US",
    if([curLanguage rangeOfString:@"ja"].location != NSNotFound){//中文和繁体
        curFontSize = 20.0;
    }
    
    NSString *strLeft = NSLocalizedString(@"guide_welcome",nil);
    NSString *strRight = NSLocalizedString(@"guide_start_sys_app_name",nil);
    
    NSString *totalStr = [[NSString alloc] initWithFormat:@"%@ %@",strLeft,strRight];
    NSMutableAttributedString *noteStr = [[NSMutableAttributedString alloc] initWithString:totalStr];
    
    NSRange redRange = NSMakeRange([totalStr rangeOfString:strRight].location, [totalStr rangeOfString:strRight].length);
    [noteStr addAttribute:NSForegroundColorAttributeName value:HW0458E3Color range:redRange];
    
    /*左头部标签*/
    UILabel *topLeftLabel = [[UILabel alloc] init];
    [topLeftLabel setFont:[UIFont boldSystemFontOfSize:curFontSize]];
    [topLeftLabel setTextAlignment:(NSTextAlignmentLeft)];
    [topLeftLabel setTextColor:HW0A132BColor];
    //[topLeftLabel setText:NSLocalizedString(@"guide_welcome",nil)];
    topLeftLabel.attributedText = noteStr;
    [bgImageView addSubview:topLeftLabel];
    [topLeftLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(H_STATE_BAR + 86.f);
//        make.left.mas_equalTo(0.f);
//        make.right.equalTo(bgImageView.mas_centerX).offset(-3);
        
        make.left.mas_equalTo(20.f);
        make.right.mas_equalTo(-20.f);
        make.height.mas_equalTo(40.f);
    }];
    
    /*右头部标签*/
//    UILabel *topRightLabel = [[UILabel alloc] init];
//    [topRightLabel setFont:[UIFont boldSystemFontOfSize:curFontSize]];
//    [topRightLabel setTextColor:HW0458E3Color];
//    [topRightLabel setText:NSLocalizedString(@"guide_start_sys_app_name",nil)];
//    [bgImageView addSubview:topRightLabel];
//    [topRightLabel mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.mas_equalTo(H_STATE_BAR + 86.f);
//        make.right.mas_equalTo(0.f);
//        make.left.equalTo(bgImageView.mas_centerX).offset(3);
//    }];
    
    /*详情标签*/
    UILabel *middleLabel = [[UILabel alloc] init];
    [middleLabel setTextColor:HW0A132BColor];
    [middleLabel setNumberOfLines:0];
    [middleLabel setTextAlignment:(NSTextAlignmentCenter)];
    [middleLabel setFont:[UIFont systemFontOfSize:20.f]];
    [middleLabel setText:tips];
    [bgImageView addSubview:middleLabel];
    [middleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topLeftLabel.mas_bottom).offset(19.f);
        make.right.mas_equalTo(-20.f);
        make.left.mas_equalTo(20.f);
    }];
    
    /*中间图片*/
    UIImage *middleImage = [UIImage imageNamed:imageName];
    UIImageView *middleImageView = [[UIImageView alloc] initWithImage:middleImage];
    [bgImageView addSubview:middleImageView];
    [middleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(middleLabel.mas_bottom).offset(8.f);
        make.left.mas_equalTo(0);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo((middleImage.size.height/middleImage.size.width)*SCREEN_W);
    }];
    
    /*底部标签*/
    UILabel *bottomLabel = [[UILabel alloc] init];
    [bottomLabel setTextColor:HW9D9D9DColor];
    [bottomLabel setNumberOfLines:0];
    [bottomLabel setTextAlignment:(NSTextAlignmentCenter)];
    [bottomLabel setFont:[UIFont systemFontOfSize:14.f]];
    [bottomLabel setText:NSLocalizedString(@"guide_set_sys_value",nil)];
    [bgImageView addSubview:bottomLabel];
    [bottomLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(-20.f);
        make.left.mas_equalTo(20.f);
        make.bottom.mas_equalTo(-24.f);
    }];
    
    for (NSInteger nfori = 0; nfori < 4; nfori++) {
        UIView *pointView = [[UIView alloc] init];
        [pointView setBackgroundColor:[UIColor whiteColor]];
        [pointView.layer setCornerRadius:2.f];
        [bgImageView addSubview:pointView];
        [pointView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.mas_equalTo(4.f);
            make.height.mas_equalTo(4.f);
            make.bottom.mas_equalTo(-51.f);
            make.left.equalTo(bgImageView.mas_centerX).offset((nfori - 2)*(8+4) + 4.f);
        }];
    }
    
    pointSelectView = [[UIView alloc] init];
    [pointSelectView setBackgroundColor:HW058DFBColor];
    [pointSelectView.layer setCornerRadius:2.f];
    [bgImageView addSubview:pointSelectView];
    [pointSelectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(4.f);
        make.height.mas_equalTo(4.f);
        make.bottom.mas_equalTo(-51.f);
        make.left.equalTo(bgImageView.mas_centerX).offset((index - 2)*(8+4) + 4.f);
    }];
    
    /*底部进度条*/
//    HW01B7EAColor
    CAGradientLayer *gl = [CAGradientLayer layer];
    gl.frame = CGRectMake(0,0,(index + 1)*SCREEN_W/4.f,8);
    gl.startPoint = CGPointMake(0, 0.5);
    gl.endPoint = CGPointMake(1, 0.5);
    gl.colors = @[(__bridge id)HW02DCF6Color.CGColor, (__bridge id)HW01B7EAColor.CGColor];

    gl.locations = @[@(0), @(1.0f)];
    
    UIView *prossView = [[UIView alloc] initWithFrame:(CGRectMake(0, 0, 25.f, 2.f))];
    [prossView setBackgroundColor:[UIColor clearColor]];
    [prossView.layer addSublayer:gl];
    [bgImageView addSubview:prossView];
    [prossView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo((index + 1)*SCREEN_W/4.f);
        make.height.mas_equalTo(8.f);
        make.bottom.mas_equalTo(0.f);
        make.left.mas_equalTo(0.f);
    }];
}

#pragma mark 点击按钮
-(void)didClickButFun:(UIButton*)but
{
    if(_didClickButton){
        _didClickButton();
    }
}
@end
