//
//  CircularProgressBar.m
//  TestGameWeb
//
//  Created by Mac on 2024/1/30.
//

#import "CircularProgressBar.h"
#import <Foundation/Foundation.h>

@implementation CircularProgressBar
{
    UIImageView* imageView;
    CAShapeLayer* maskLayer;
    float currentValue;
}

-(void)updateUI{
    [self makeGradient];
    [self setUpMask];
}
-(void)animateCircleWithStrokeEnd:(float)end{
//    let oldStrokeEnd = maskLayer.strokeEnd
//    maskLayer.strokeEnd = strokeEnd
//
//    //override strokeEnd implicit animation
//    let animation = CABasicAnimation(keyPath: "strokeEnd")
//    animation.duration = 0.5
//    animation.fromValue = oldStrokeEnd
//    animation.toValue = strokeEnd
//    animation.timingFunction = CAMediaTimingFunction(name: CAMediaTimingFunctionName.easeInEaseOut)
//    maskLayer.add(animation, forKey: "animateCircle")
//
//    currentValue = strokeEnd
}
-(void)setProgress:(float)value{
    maskLayer.strokeEnd = value;
}

-(void)makeGradient{
    imageView =  [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"12"]];
    imageView.frame = CGRectMake(0,0,self.frame.size.width, self.frame.size.height);
    imageView.contentMode = UIViewContentModeScaleToFill;
    [self addSubview:imageView];
}

-(void)setUpMask{
    UIBezierPath*circlePath = [UIBezierPath bezierPathWithArcCenter:CGPointMake(self.frame.size.width / 2.0,self.frame.size.height / 2.0) radius:(self.frame.size.width - 10)/2 startAngle:-M_PI/2 endAngle:M_PI*1.5 clockwise:YES];
    maskLayer = [CAShapeLayer layer];
    [maskLayer setPath: circlePath.CGPath];
    maskLayer.fillColor = UIColor.clearColor.CGColor;
    maskLayer.strokeColor = UIColor.redColor.CGColor;
    maskLayer.lineWidth = 8.0;
    maskLayer.strokeEnd = 0;
    maskLayer.lineCap = kCALineCapButt;// CAShapeLayerLineCap;
    imageView.layer.mask = maskLayer;
}

@end
