#import "BottomTabsBasePresenter.h"
#import "RNNBottomTabsController.h"
#import "RNNConvert.h"
#import "UIImage+utils.h"

@implementation BottomTabsBasePresenter {
    BOOL _didApplyInitialTabBarVisibility;
}

- (BOOL)tabBarVisibilityAnimation:(BOOL)animated {
    if (@available(iOS 18.0, *)) {
        return animated;
    }
    if (_didApplyInitialTabBarVisibility) {
        return animated;
    }

    _didApplyInitialTabBarVisibility = YES;
    return NO;
}

- (void)applyOptionsOnInit:(RNNNavigationOptions *)options {
    [super applyOptionsOnInit:options];
    RNNBottomTabsController *bottomTabs = self.tabBarController;
    RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];
    if (@available(iOS 18.0, *)) {
        [bottomTabs setTabBarVisible:[withDefault.bottomTabs.visible withDefault:YES]
                            animated:NO];
    }
    [bottomTabs setCurrentTabIndex:[withDefault.bottomTabs.currentTabIndex withDefault:0]];
    if (withDefault.bottomTabs.currentTabId.hasValue) {
        [bottomTabs setCurrentTabID:withDefault.bottomTabs.currentTabId.get];
    }
    if ([[withDefault.bottomTabs.titleDisplayMode withDefault:@"alwaysShow"]
            isEqualToString:@"alwaysHide"]) {
        [bottomTabs centerTabItems];
    }
}

- (void)applyOptions:(RNNNavigationOptions *)options {
    RNNBottomTabsController *bottomTabs = self.tabBarController;
    RNNNavigationOptions *withDefault = [options withDefault:[self defaultOptions]];

    [bottomTabs setTabBarTestID:[withDefault.bottomTabs.testID withDefault:nil]];
    [bottomTabs reconcileTabBarVisible:[withDefault.bottomTabs.visible withDefault:YES]
                              animated:[self tabBarVisibilityAnimation:
                                                 [withDefault.bottomTabs.animate withDefault:YES]]];

    [bottomTabs.view setBackgroundColor:[withDefault.layout.backgroundColor withDefault:nil]];
    [bottomTabs setTabBarHideShadow:[withDefault.bottomTabs.hideShadow withDefault:NO]];
    if (options.bottomTabs.barStyle.hasValue) {
        [bottomTabs setTabBarStyle:[RNNConvert UIBarStyle:options.bottomTabs.barStyle.get]];
    }
    [self applyBackgroundColor:[withDefault.bottomTabs.backgroundColor withDefault:nil]
                   translucent:[withDefault.bottomTabs.translucent withDefault:NO]];
    [self applyTabBarBorder:withDefault.bottomTabs];
    [self applyTabBarShadow:withDefault.bottomTabs.shadow];
}

- (void)mergeOptions:(RNNNavigationOptions *)mergeOptions
     resolvedOptions:(RNNNavigationOptions *)currentOptions {
    [super mergeOptions:mergeOptions resolvedOptions:currentOptions];
    RNNBottomTabsController *bottomTabs = self.tabBarController;
    RNNNavigationOptions *withDefault = [mergeOptions withDefault:[self defaultOptions]];

    if (mergeOptions.bottomTabs.currentTabIndex.hasValue) {
        [bottomTabs setCurrentTabIndex:mergeOptions.bottomTabs.currentTabIndex.get];
        [mergeOptions.bottomTabs.currentTabIndex consume];
    }

    if (mergeOptions.bottomTabs.currentTabId.hasValue) {
        [bottomTabs setCurrentTabID:mergeOptions.bottomTabs.currentTabId.get];
        [mergeOptions.bottomTabs.currentTabId consume];
    }

    if (mergeOptions.bottomTabs.testID.hasValue) {
        [bottomTabs setTabBarTestID:mergeOptions.bottomTabs.testID.get];
    }

    if (mergeOptions.bottomTabs.backgroundColor.hasValue) {
        [self setTabBarBackgroundColor:mergeOptions.bottomTabs.backgroundColor.get];
    }

    if (mergeOptions.bottomTabs.barStyle.hasValue) {
        [bottomTabs setTabBarStyle:[RNNConvert UIBarStyle:mergeOptions.bottomTabs.barStyle.get]];
    }

    if (mergeOptions.bottomTabs.translucent.hasValue) {
        [self setTabBarTranslucent:mergeOptions.bottomTabs.translucent.get];
    }

    if (mergeOptions.bottomTabs.hideShadow.hasValue) {
        [bottomTabs setTabBarHideShadow:mergeOptions.bottomTabs.hideShadow.get];
    }

    if (mergeOptions.bottomTabs.visible.hasValue) {
        [bottomTabs setTabBarVisible:mergeOptions.bottomTabs.visible.get
                            animated:[withDefault.bottomTabs.animate withDefault:YES]];
    }

    if (mergeOptions.layout.backgroundColor.hasValue) {
        [bottomTabs.view setBackgroundColor:mergeOptions.layout.backgroundColor.get];
    }

    if (mergeOptions.bottomTabs.borderColor.hasValue ||
        mergeOptions.bottomTabs.borderWidth.hasValue) {
        [self applyTabBarBorder:withDefault.bottomTabs];
    }

    if (mergeOptions.bottomTabs.shadow.hasValue) {
        [self applyTabBarShadow:withDefault.bottomTabs.shadow];
    }
}

- (RNNBottomTabsController *)tabBarController {
    return (RNNBottomTabsController *)self.boundViewController;
}

- (UITabBar *)tabBar {
    return self.tabBarController.tabBar;
}

- (void)applyTabBarBorder:(RNNBottomTabsOptions *)options {
    if (options.borderColor.hasValue || options.borderWidth.hasValue) {
        self.tabBar.backgroundImage = [UIImage new];
        self.tabBar.shadowImage = [UIImage
            imageWithSize:CGSizeMake(1, [[options.borderWidth withDefault:@(1)] floatValue])
                    color:[options.borderColor withDefault:UIColor.blackColor]];
    }
}

- (void)applyTabBarShadow:(RNNShadowOptions *)options {
    self.tabBar.layer.shadowRadius =
        [options.radius withDefault:@(self.tabBar.layer.shadowRadius)].floatValue;
    self.tabBar.layer.shadowColor =
        [options.color withDefault:[UIColor colorWithCGColor:self.tabBar.layer.shadowColor]]
            .CGColor;
    self.tabBar.layer.shadowOpacity =
        [options.opacity withDefault:@(self.tabBar.layer.shadowOpacity)].floatValue;
}

- (void)applyBackgroundColor:(UIColor *)backgroundColor translucent:(BOOL)translucent {
}

- (void)setTabBarBackgroundColor:(UIColor *)backgroundColor {
}

- (void)setTabBarTranslucent:(BOOL)translucent {
}

@end
