//
//  YHMainApplicantInformationViewController.swift
//  galaxy
//
//  Created by EDY on 2024/1/25.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHMainApplicantInformationViewController: YHBaseViewController {
    
    var stepView: YHStepView!
    var bottomView: YHBottomNextView!
    var tableView: UITableView!
    var viewModel: YHMainApplicantInformationViewModel!
    var dataSource: [YHSectionItemModel]?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel = YHMainApplicantInformationViewModel()
        gk_navTitle = "主申请人信息填写"
        setView()
        getData()
        // Do any additional setup after loading the view.
    }
    
    func getData() {
        viewModel.requestMainInformation("133969") {[weak self] success, error in
            guard let self = self else { return }
            if self.stepView.currentIndex == 0 {
                self.dataSource = self.viewModel.getBaseDataSource()
            } else if self.stepView.currentIndex == 1 {
                self.dataSource = self.viewModel.getIDCardDataSource()
            } else if self.stepView.currentIndex == 2 {
                self.dataSource = self.viewModel.getDocumentInformation()
            } else if self.stepView.currentIndex == 3 {
                self.dataSource = self.viewModel.getDetailDataSource()
            }
            self.tableView.reloadData()
        }
    }
    
    func setView() {
        stepView = {
            let step = YHStepView()
            step.dataSource = ["初始信息", "证件上传", "基本信息", "证件信息"]
            step.block = { [weak self] (index) in
                guard let self = self else { return }
                self.getData()
            }
            return step
        }()
        view.addSubview(stepView)
        stepView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar)
            make.left.right.equalTo(view)
            make.height.equalTo(104)
        }
        
        tableView = {
            let tableView = UITableView(frame:.zero, style:.plain)
            if #available(iOS 11.0, *) {
                tableView.contentInsetAdjustmentBehavior = .never
            }
            tableView.backgroundColor = .clear
            tableView.separatorStyle = .none
            tableView.delegate = self
            tableView.dataSource = self
            tableView.register(cellWithClass: YHMainInformationTableViewCell.self)
            tableView.register(cellWithClass: YHMainInformationCardTableViewCell.self)
            return tableView
        }()
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar + 104)
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom() - 64)
            make.left.right.bottom.equalTo(view)
        }
        
        bottomView = {
            let bottom = YHBottomNextView()
            bottom.block = { [weak self] in
                guard let self = self else { return }
                self.stepView.currentIndex = self.stepView.currentIndex + 1
                self.getData()
            }
            return bottom
        }()
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(k_Height_safeAreaInsetsBottom() + 64)
        }
    }
    
}

extension YHMainApplicantInformationViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if stepView.currentIndex == 1 {
            let cell = tableView.dequeueReusableCell(withClass: YHMainInformationCardTableViewCell.self)
            cell.dataSource = dataSource?[indexPath.row]
            cell.type = YHCardType(rawValue: indexPath.row)
            return cell
        } else {
            let cell = tableView.dequeueReusableCell(withClass: YHMainInformationTableViewCell.self)
            cell.dataSource = dataSource?[indexPath.row]
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        let count = dataSource?[indexPath.row].models?.count ?? 0
        if stepView.currentIndex == 1 {
            return 217.0
        } else {
            return CGFloat(count * 52 + 52 + 14)
        }
    }
}
