//
//  YHWorkExperienceViewController.swift
//  galaxy
//
//  Created by EDY on 2024/2/19.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit

class YHWorkExperienceViewController: YHBaseViewController {

    var stepView: YHStepView!
    var bottomView: YHBottomNextView!
    var tableView: UITableView!
    var viewModel: YHWorkExperienceViewModel!
    var baseDataSource: [YHSectionWorkExperienceModel]?
    var introductionDataSource: [YHWorkItemListModel]?
    var fileDataSource: [String]?
    var workExperienceID: String?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        viewModel = YHWorkExperienceViewModel()
        gk_navTitle = "工作经验信息填写"
        setView()
        getData()
        // Do any additional setup after loading the view.
    }
    
    func getData() {
        viewModel.requestWorkExample { success, error in
            
        }
        viewModel.requestWorkDetail(workExperienceID ?? "") {[weak self] success, error in
            guard let self = self else { return }
            self.updateDataSource()

        }
        updateDataSource()
    }
    
    func updateDataSource() {
        if self.stepView.currentIndex == 0 {
            self.baseDataSource = self.viewModel.getBaseDataSource()
        }
        self.tableView.reloadData()
    }
    
    func setView() {
        stepView = {
            let step = YHStepView()
            step.dataSource = ["基本信息", "工作职责", "工作亮点", "项目介绍", "简历/工作总结"]
            step.contentSize = CGSize(width: KScreenWidth * 1.2, height: 104)
            step.showsHorizontalScrollIndicator = false
            step.showsVerticalScrollIndicator = false
            step.block = { [weak self] (index) in
                guard let self = self else { return }
                self.updateDataSource()
            }
            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: YHWorkExperienceTableViewCell.self)
            tableView.register(cellWithClass: YHWorkResponsibilitiesTableViewCell.self)
            tableView.register(cellWithClass: YHWorkHighlightsTableViewCell.self)
            tableView.register(cellWithClass: YHWorkIntroductionTableViewCell.self)
            tableView.register(cellWithClass: YHWorkMessageSelectTableViewCell.self)
            tableView.register(cellWithClass: YHWorkFileSyncTableViewCell.self)
            return tableView
        }()
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.top.equalTo(k_Height_NavigationtBarAndStatuBar + YHStepView.height)
            make.bottom.equalTo(-k_Height_safeAreaInsetsBottom() - 64)
            make.left.right.bottom.equalTo(view)
        }
        
        bottomView = {
            let bottom = YHBottomNextView()
            bottom.nextblock = { [weak self] in
                guard let self = self else { return }
                self.stepView.currentIndex = self.stepView.currentIndex + 1
                self.updateDataSource()
            }
            bottom.saveBlock = { [weak self] in
                guard let self = self else { return }
                
            }
            return bottom
        }()
        view.addSubview(bottomView)
        bottomView.snp.makeConstraints { make in
            make.left.right.bottom.equalTo(view)
            make.height.equalTo(k_Height_safeAreaInsetsBottom() + 64)
        }
    }
    
}

extension YHWorkExperienceViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        if stepView.currentIndex == 1 || stepView.currentIndex == 2 || stepView.currentIndex == 3 {
            return 1
        }
        if stepView.currentIndex == 4 {
            return 2
        }
        return baseDataSource?.count ?? 0
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        if stepView.currentIndex == 1 {
            let cell = tableView.dequeueReusableCell(withClass: YHWorkResponsibilitiesTableViewCell.self)
            cell.exampleBlock = {[weak self] in
                let mainVc = YHWorkExampleViewController()
                mainVc.dataSource = self?.viewModel.exampleModels
                mainVc.type = .wduty
                let vc = UINavigationController(rootVC: mainVc)
                vc.modalPresentationStyle = .pageSheet
                self?.navigationController?.present(vc, animated: true)
            }
            return cell
        } else if stepView.currentIndex == 2 {
            let cell = tableView.dequeueReusableCell(withClass: YHWorkHighlightsTableViewCell.self)
            cell.exampleBlock = {[weak self] in
                let mainVc = YHWorkExampleViewController()
                mainVc.dataSource = self?.viewModel.exampleModels
                mainVc.type = .highlight
                let vc = UINavigationController(rootVC: mainVc)
                vc.modalPresentationStyle = .pageSheet
                self?.navigationController?.present(vc, animated: true)
            }
            return cell
        } else if stepView.currentIndex == 3 {
            let cell = tableView.dequeueReusableCell(withClass: YHWorkIntroductionTableViewCell.self)
            cell.addIntroductionBlock = {[weak self] in
                guard let self = self else { return }
                let vc = YHWorkIntroductionViewController()
                self.navigationController?.pushViewController(vc, animated: true)
            }
            return cell
        } else if stepView.currentIndex == 4 {
            if indexPath.row == 0 {
                let cell = tableView.dequeueReusableCell(withClass: YHWorkMessageSelectTableViewCell.self)
                return cell
            } else {
                let cell = tableView.dequeueReusableCell(withClass: YHWorkFileSyncTableViewCell.self)
                return cell
            }
        } else {
            let cell = tableView.dequeueReusableCell(withClass: YHWorkExperienceTableViewCell.self)
            cell.dataSource = baseDataSource?[indexPath.row]
            cell.workExperienceBlock = {[weak self] model in
                guard let self = self else { return }
                self.viewModel.updateModel(model)
                self.updateDataSource()
            }
            return cell
        }
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        if stepView.currentIndex == 1 || stepView.currentIndex == 2 {
            return KScreenHeight - k_Height_NavigationtBarAndStatuBar - YHStepView.height - k_Height_safeAreaInsetsBottom() - 64
        } else if stepView.currentIndex == 3 {
            let number = introductionDataSource?.count ?? 0
            return CGFloat(76 * number + 161)
        } else if stepView.currentIndex == 4 {
            if indexPath.row == 0 {
                return 166
            } else {
                let count = fileDataSource?.count ?? 0
                return CGFloat(52 * count + 138)
            }
        } else {
            let array = baseDataSource?[indexPath.row].models ?? []
            var h = 0
            for i in 0..<array.count {
                let data = array[i]
                if data.id == .id3 || data.id == .id4 || data.id == .id18 {
                    h = h + 132
                } else if data.id == .id13 {
                    h = h + 72
                } else {
                    h = h + 52
                }
            }
            return CGFloat(h + 52 + 14)
        }
    }
}
