//
//  JCStaticTableViewController.m
//  EZOpenSDKDemo
//
//  Created by JuneCheng on 2026/3/12.
//  Copyright © 2026 hikvision. All rights reserved.
//

#import "JCStaticTableViewController.h"

#import "JCFormItem.h"
#import "JCFormArrowItem.h"
#import "JCFormSectionItem.h"

#import "JCFormCell.h"
#import "JCTextFieldFormCell.h"
#import "JCTextViewFormCell.h"
#import "JCSwitchFormCell.h"

@interface JCStaticTableViewController ()

@end

@implementation JCStaticTableViewController

- (instancetype)init {
    return [super initWithStyle:UITableViewStyleGrouped];
}

#pragma mark - 生命周期

- (void)viewDidLoad {
    [super viewDidLoad];
}

#pragma mark - UITabelViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.sections.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.sections[section].items.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    JCFormItem *item = self.sections[indexPath.section].items[indexPath.row];
    UITableViewCell *cell;
    if ([item isKindOfClass:[JCFormTextViewItem class]]) {// 多行文本输入
        cell = [JCTextViewFormCell cellWithTableView:tableView cellStyle:UITableViewCellStyleDefault];
        ((JCTextViewFormCell *)cell).item = item;
    } else if ([item isKindOfClass:[JCFormTextFieldItem class]]) {// 单行文本输入
        cell = [JCTextFieldFormCell cellWithTableView:tableView cellStyle:UITableViewCellStyleDefault];
        ((JCTextFieldFormCell *)cell).item = item;
    } else if ([item isKindOfClass:[JCFormSwitchItem class]]) {// 开关
        cell = [JCSwitchFormCell cellWithTableView:tableView cellStyle:UITableViewCellStyleDefault];
        ((JCSwitchFormCell *)cell).item = item;
    }  else {// 其他
        cell = [JCFormCell cellWithTableView:tableView cellStyle:UITableViewCellStyleValue1];
        ((JCFormCell *)cell).item = item;
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    JCFormItem *item = self.sections[indexPath.section].items[indexPath.row];
    if (item.itemClick) {// 普通的cell
        item.itemClick(indexPath);
    } else if([item isKindOfClass:[JCFormArrowItem class]]) {// 带箭头的cell
        JCFormArrowItem *arrowItem = (JCFormArrowItem *)item;
        if (arrowItem.destVC) {
            UIViewController *vc = [[arrowItem.destVC alloc] init];
            if ([vc isKindOfClass:[UIViewController class]]) {
                vc.navigationItem.title = arrowItem.title;
                [self.navigationController pushViewController:vc animated:YES];
            }
        }
    }
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section {
    return self.sections[section].headerTitle;
}

- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section {
    return self.sections[section].footerTitle;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return self.sections[indexPath.section].items[indexPath.row].cellHeight;
}

#pragma mark - 自定义方法

- (JCStaticTableViewController *(^)(JCFormItem *))addItem {
    __weak typeof(self) weakSelf = self;
    if (!self.sections.firstObject) {
        [self.sections addObject:[JCFormSectionItem sectionWithItems:@[] headerTitle:nil footerTitle:nil]];
    }
    return ^(JCFormItem *item) {
        [weakSelf.sections.firstObject.items addObject:item];
        return weakSelf;
    };
}

#pragma mark - 懒加载

- (NSMutableArray<JCFormSectionItem *> *)sections {
    if (!_sections) {
        _sections = [NSMutableArray array];
    }
    return _sections;
}

@end
