//
//  ChatLongPressMenuView.m
//  AIIM
//
//  Created by qitewei on 2025/5/7.
//

#import "ChatLongPressMenuView.h"

@interface ChatLongPressMenuView()
@property (nonatomic, copy) NSArray * titles;
@end

@implementation ChatLongPressMenuView

- (instancetype)initWithFrame:(CGRect)frame{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = UIColor.whiteColor;
        [self configUI];
    }
    return self;
}


- (instancetype)initWithMessageType:(NSInteger)type isSender:(BOOL)isSender{
    NSMutableArray * tempArray = [NSMutableArray array];
    if (isSender) {
        if (type == 0) {
            [tempArray addObjectsFromArray:@[NSLocalizedString(@"cellact-copy", @""),
                                             NSLocalizedString(@"cellact-deleat", @""),
                                             NSLocalizedString(@"cellact-zhuanfa", @""),
                                             NSLocalizedString(@"cellact-shouchang", @""),
                                             NSLocalizedString(@"cellact-chehui", @""),
                                             NSLocalizedString(@"cellact-duoxuan", @""),
                                             NSLocalizedString(@"cellact-yinyong", @"")]];
        }else{
            [tempArray addObjectsFromArray:@[NSLocalizedString(@"cellact-deleat", @""),
                                             NSLocalizedString(@"cellact-zhuanfa", @""),
                                             NSLocalizedString(@"cellact-shouchang", @""),
                                             NSLocalizedString(@"cellact-chehui", @""),
                                             NSLocalizedString(@"cellact-duoxuan", @""),
                                             NSLocalizedString(@"cellact-yinyong", @"")]];
        }
    }else{
        if (type == 0) {
            [tempArray addObjectsFromArray:@[NSLocalizedString(@"cellact-copy", @""),
                                             NSLocalizedString(@"cellact-deleat", @""),
                                             NSLocalizedString(@"cellact-zhuanfa", @""),
                                             NSLocalizedString(@"cellact-shouchang", @""),
                                             NSLocalizedString(@"cellact-duoxuan", @""),
                                             NSLocalizedString(@"cellact-yinyong", @"")]];
        }else{
            [tempArray addObjectsFromArray:@[NSLocalizedString(@"cellact-save", @""),
                                             NSLocalizedString(@"cellact-deleat", @""),
                                             NSLocalizedString(@"cellact-zhuanfa", @""),
                                             NSLocalizedString(@"cellact-shouchang", @""),
                                             NSLocalizedString(@"cellact-duoxuan", @""),
                                             NSLocalizedString(@"cellact-yinyong", @"")]];
        }
        
    }
    
    _titles = tempArray.copy;
    tempArray = nil;
    
    CGRect rect = CGRectMake(0, 0, 290, 110);
    if (self = [super initWithFrame:rect]) {
        self.backgroundColor = UIColor.whiteColor;
        [self configUI];
    }
    
    return self;
}

- (void)configUI{
    
    NSMutableArray * buttonArray = [NSMutableArray array];
    NSMutableArray * button2Array = [NSMutableArray array];
    for (NSString * title in self.titles) {
        UIButton * item = [UIButton buttonWithType:UIButtonTypeCustom];
        item.tag = [self.titles indexOfObject:title]+3000;
        [item setTitle:title forState:UIControlStateNormal];
        [item setTitleColor:UIColor.blackColor forState:UIControlStateNormal];
        item.titleLabel.font = SYSFONT(16);
        [item addTarget:self action:@selector(menuButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:item];
        if ([self.titles indexOfObject:title] <= 3) {
            [buttonArray addObject:item];
            [item mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(40);
                make.top.mas_equalTo(10);
            }];
        }else{
            [button2Array addObject:item];
            [item mas_makeConstraints:^(MASConstraintMaker *make) {
                make.height.mas_equalTo(40);
                make.top.mas_equalTo(60);
            }];
            
            if ([self.titles indexOfObject:title] == self.titles.count - 1 && button2Array.count < 4) {
                for (NSInteger i = self.titles.count; i <= 7; i++) {
                    UIView * place = [UIView new];
                    place.backgroundColor = UIColor.whiteColor;
                    [self addSubview:place];
                    [button2Array addObject:place];
                    [place mas_makeConstraints:^(MASConstraintMaker *make) {
                        make.height.mas_equalTo(40);
                        make.top.mas_equalTo(60);
                    }];
                }
                
            }
        }
    }
    
    [buttonArray mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:20 tailSpacing:20];
    [button2Array mas_distributeViewsAlongAxis:MASAxisTypeHorizontal withFixedSpacing:30 leadSpacing:20 tailSpacing:20];
}

#pragma mark event
- (void)menuButtonClicked:(UIButton *)button{
    NSInteger index = 0;
    NSString * titleString = self.titles[button.tag-3000];
    if ([titleString isEqualToString:NSLocalizedString(@"cellact-copy", @"")]) {
        index = 0;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-deleat", @"")]) {
        index = 1;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-zhuanfa", @"")]) {
        index = 2;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-shouchang", @"")]) {
        index = 3;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-chehui", @"")]) {
        index = 4;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-duoxuan", @"")]) {
        index = 5;
    }else if ([titleString isEqualToString:NSLocalizedString(@"cellact-yinyong", @"")]) {
        index = 6;
    }
    else if ([titleString isEqualToString:NSLocalizedString(@"cellact-save", @"")]) {
        index = 7;
    }
    
    
    
    if (self.indexOfMenuClicked) {
        self.indexOfMenuClicked(index);
    }
}

@end
