//
//  YHFamilyInitialInfo.swift
//  galaxy
//
//  Created by edy on 2024/1/23.
//  Copyright © 2024 https://www.galaxy-immi.com. All rights reserved.
//

import UIKit
import SmartCodable

protocol YHFormItemProtocol {
    
    func getTitle() -> String
    func getSubTitle() -> String
}

// 表单标题类型
enum YHFormTitleItemType:Int, CaseIterable {
    
    // 配偶
    case sponse = 1
    // 父母
    case parent
    // 子女
    case child
    // 兄弟姐妹
    case brother
    
    // 随行
    case accompany
    // 国籍
    case nationality
    // 居住信息
    case liveInfo
    // 港澳通信证
    case hkAndMacaoPassport
    
    // 中国身份证
    case chinaIdCardInfo
    // 护照及其他旅行证件
    case passPortOrTravelInfo
    
    // 配偶信息
    case spouseInfo
    // 职业信息
    case occupationInfo
    // 学历学位信息
    case degreeInfo
    // 在港信息
    case inHongKongInfo
    // 香港身份证
    case hkIdentityCardInfo
    
    // 父亲信息
    case fatherInfo
    // 母亲信息
    case motherInfo
    // 基本信息
    case basicInfo
    // 子女信息
    case childInfo
    // 是否前次婚姻子女
    case isPreviousMarriageChild
    // 兄弟姐妹信息
    case brotherInfo
    // 主申请人学历
    case mainApplicantEducation
    // 主申请人专业资格及会员资格
    case mainApplicantProfessionalQualification
    // 学历证书姓名及出生日期正确性
    case edutionalCertificateNameAndBirthday


    
}

// 表单添加条目类型
enum YHFormAddItemType:Int {
    
    case addChild = 1
    case addBrother
    case addDegree
    case addEducation
    case addCertificateInfo
}

// 表单具体条目类型
enum YHFormDetailItemType:Int {
    
    // 是否随行至香港
    case isAccompanyToHK =  1
    // 是否与主申请人同住
    case isLiveTother
    // 国家/地区
    case nationOrArea
    // 现居住城市
    case liveCity
    // 详细地址
    case detailAddress
    // 是否在海外居住满1年及以上
    case isLiveOverSeasMore1Year
    // 是否办理
    case isHandleHKPassPort
    
    // 证件类别
    case certificateType
    // 证件号码
    case certificateNumber
    // 中国身份证号
    case chinaIdentityCardNumber
    // 旅行证件号码
    case traverlPassportNumber
    // 签发日期
    case certificateSignDate
    // 届满日期
    case certificateValidDate
    // 签发地
    case certificateSignPlace
    
    // 配偶姓名
    case spouseName
    // 曾用名
    case everName
    // 婚前姓氏
    case befourMarryFirstName
    // 出生日期
    case birthday
    // 出生国家/地区
    case birthNation
    // 出生城市
    case birthCity
    // 职业
    case occupation
    // 职业名称
    case occupationName
    // 是否拥有学位
    case ownDegree
    // 目前是否在港
    case isNowInHK
    // 获准逗留至
    case stayHKDate
    // 在港身份
    case roleInHK
    // 是否办理过(香港身份证)
    case isHaveHkIdentityCard
    // 香港身份证号码
    case hkIdentityCardNumber
    // 学位信息X
    case degreeDetailInfo
    
    // 父亲姓名
    case fatherName
    // 母亲姓名
    case motherName
    // 婚姻状况
    case marriageState
    // 现居住国家/地区
    case liveNationOrArea
    // 仅有两个按钮 标题数组有外部提供
    case onlyDoubleChoice
    // 主申请人和现配偶是否拥有孩子抚养权
    case ownCustody
    // 子女姓名
    case childName
    // 性别
    case gender
    // 是否为前次婚姻子女
    case isPreviousMarriageChild
    // 兄弟姐妹姓名
    case brotherName
    
    // 学历信息相关
    case educationInfo
    // 学校全称
    case universityFullName
    // 授课形式
    case educationStyle
    // 入学年月
    case educationTime
    // 毕业年月
    case graduateTime
    // 专业
    case educationMajor
    // 学位
    case educationDegree
    // 学位类型
    case degreeType
    // 上课国家/地区
    case educationCountry
    // 上课城市
    case educationCity
    
    // 证书信息
    case certificateInfo
    // 资格证书名称
    case qualificationCertificateName
    // 颁授机构全称
    case conferAgencyFullName
    // 颁授年份
    case conferTime
    // 颁授国家/地区
    case conferCountry
    // 颁授城市
    case conferCity
}

//添加item
class YHFormAddItem : YHFormItemProtocol
{
    var type: YHFormAddItemType
    
    init(type: YHFormAddItemType) {
        self.type = type
    }
    
    func getTitle() -> String {
        switch self.type {
        case .addChild:
            return "新增子女".local
        case .addBrother:
            return "新增兄弟姐妹".local
        case .addDegree:
            return "增加学位信息".local
        case .addEducation:
            return "新增学历信息".local
        case .addCertificateInfo:
            return "新增证书信息".local
        }
    }
    
    func getSubTitle() -> String {
        return ""
    }
}


// 标题item
class YHFormTitleItem : YHFormItemProtocol {
    
    var type: YHFormTitleItemType
    
    init(type: YHFormTitleItemType) {
        self.type = type
    }
    
    func getTitle() -> String {
        switch self.type {
        case .sponse:
            return "配偶".local
        case .parent:
            return "父母".local
        case .brother:
            return "兄弟姐妹".local
        case .child:
            return "子女".local
        case .accompany:
            return "随行".local
        case .nationality:
            return "国籍".local
        case .liveInfo:
            return "居住信息".local
        case .hkAndMacaoPassport:
            return "港澳通行证".local
        case .chinaIdCardInfo:
            return "中国身份证".local
        case .passPortOrTravelInfo:
            return String(format: "%@(%@)", "护照及其他旅行证件".local, "选填".local)
        case .spouseInfo:
            return "配偶信息".local
        case .occupationInfo:
            return "职业信息".local
        case .degreeInfo:
            return "学历学位信息".local
        case .inHongKongInfo:
            return "在港信息".local
        case .hkIdentityCardInfo:
            return "香港身份证".local
        case .fatherInfo:
            return "父亲信息".local
        case .motherInfo:
            return "母亲信息".local
        case .basicInfo:
            return "基本信息".local
        case .childInfo:
            return "子女信息".local
        case .isPreviousMarriageChild:
            return "是否前次婚姻子女".local
        case .brotherInfo:
            return "兄弟姐妹信息".local
        case .mainApplicantEducation:
            return "主申请人学历".local
        case .mainApplicantProfessionalQualification:
            return "主申请人专业资格及会员资格".local
        case .edutionalCertificateNameAndBirthday:
            return "学历证书姓名及出生日期正确性".local
            
        }
    }
    
    func getSubTitle() -> String {
        switch self.type {
        case .sponse:
            return String(format: "(%@)", "如未婚、离婚、丧偶可不填".local)
        case .parent:
            return String(format: "(%@)", "包含已故".local)
        case .brother:
            return String(format: "(%@)", "如无可不填".local)
        case .child:
            return String(format: "(%@)", "如无可不填".local)
        case .mainApplicantEducation:
            return "（请从大专开始写起）".local
        case .mainApplicantProfessionalQualification:
            return "(如有，非必填）".local
        case .edutionalCertificateNameAndBirthday:
            return "".local
        case .isPreviousMarriageChild:
            return "".local
        case .accompany:
            return "".local
        case .nationality:
            return "".local
        case .liveInfo:
            return "".local
        case .hkAndMacaoPassport:
            return "".local
        case .chinaIdCardInfo:
            return "".local
        case .passPortOrTravelInfo:
            return "".local
        case .spouseInfo:
            return "".local
        case .occupationInfo:
            return "".local
        case .degreeInfo:
            return "".local
        case .inHongKongInfo:
            return "".local
        case .hkIdentityCardInfo:
            return "".local
        case .fatherInfo:
            return "".local
        case .motherInfo:
            return "".local
        case .basicInfo:
            return "".local
        case .childInfo:
            return "".local
        case .brotherInfo:
            return "".local
            
            
        }
    }
}

class YHFormDegreeItem:SmartCodable {
    
    
    var title:String?
    var subTitle1:String? {
        return "学位证".local
    }
    var subTitle2:String? {
        return "颁发地区".local
    }
    var value1:String?
    var value2:String?
    
    init(title: String? = nil, value1: String? = nil, value2: String? = nil) {
        self.title = title
        self.value1 = value1
        self.value2 = value2
    }
    
    required init() {
        
    }
}

// 具体item
class YHFormDetailItem : YHFormItemProtocol {
    
    var type: YHFormDetailItemType
    // 是否是必填项
    var isNeed: Bool = true
    
    var value: String?
    // 灰色提示
    var placeHolder: String?
    // 红色提示文字
    var tips:String?
    
    // 是否需要展示Tips
    var isShowTips:Bool {
        if let value = value, !value.isEmpty {
            return false
        }
        return true
    }
    
    init(type: YHFormDetailItemType, isNeed:Bool = true) {
        self.type = type
        self.isNeed = isNeed
    }
    
    
    func getTitle() -> String {
        
        switch type {
        case .isAccompanyToHK:
            return "是否随行至香港".local
        case .isLiveTother:
            return "是否与主申请人同住".local
        case .nationOrArea:
            return String(format: "%@/%@", "国家".local, "地区".local)
        case .liveCity:
            return "现居住城市".local
        case .detailAddress:
            return "详细地址".local
        case .isLiveOverSeasMore1Year:
            return "是否在海外居住满1年及以上".local
        case .isHandleHKPassPort:
            return "是否办理".local
        case .certificateType:
            return "证件类别".local
        case .certificateNumber:
            return "证件号码".local
        case .chinaIdentityCardNumber:
            return "中国身份证号".local
        case .traverlPassportNumber:
            return "旅行证件号码".local
        case .certificateSignDate:
            return "签发日期".local
        case .certificateValidDate:
            return "届满日期".local
        case .certificateSignPlace:
            return "签发地".local
        case .spouseName:
            return "配偶姓名".local
        case .everName:
            return "曾用名".local
        case .befourMarryFirstName:
            return "婚前姓氏".local
        case .birthday:
            return "出生日期".local
        case .birthNation:
            return "出生国家/地区".local
        case .birthCity:
            return "出生城市".local
        case .occupation:
            return "职业".local
        case .occupationName:
            return "职业名称".local
        case .ownDegree:
            return "是否拥有学位".local
        case .isNowInHK:
            return "目前是否在港".local
        case .stayHKDate:
            return "获准逗留至".local
        case .roleInHK:
            return "在港身份".local
        case .isHaveHkIdentityCard:
            return "是否办理过".local
        case .hkIdentityCardNumber:
            return "香港身份证号码".local
        case .degreeDetailInfo:
            return "学位信息X".local
        case .fatherName:
            return "父亲姓名".local
        case .motherName:
            return "母亲姓名".local
        case .marriageState:
            return "婚姻状况".local
        case .liveNationOrArea:
            return "现居住国家/地区".local
        case .ownCustody:
            return "主申请人和现配偶是否拥有孩子抚养权".local
        case .childName:
            return "子女姓名".local
        case .gender:
            return "性别".local
        case .isPreviousMarriageChild:
            return "是否前次婚姻子女".local
        case .brotherName:
            return "兄弟姐妹姓名".local
        case .onlyDoubleChoice:
            return "".local
            
        case .educationInfo:
            return "".local
        case .certificateInfo:
            return "".local
        case .universityFullName:
            return "学校全称".local
        case .educationStyle:
            return "授课形式".local
        case .educationTime:
            return "入学年月".local
        case .graduateTime:
            return "毕业年月".local
        case .educationMajor:
            return "专业".local
        case .educationDegree:
            return "学位".local
        case .degreeType:
            return "学位类型".local
        case .educationCountry:
            return "上课国家/地区".local
        case .educationCity:
            return "上课城市".local
            
        case .qualificationCertificateName:
            return "资格证书名称".local
        case .conferAgencyFullName:
            return "颁授机构全称".local
        case .conferTime:
            return "颁授年份".local
        case .conferCountry:
            return "颁授国家/地区".local
        case .conferCity:
            return "颁授城市".local
        }

    }
    
    func getSubTitle() -> String {
        return ""
    }
}

/*  ***** ***** ***** ***** ***** ***** ***** ***** */
// MARK: - YHWelcome
class YHWelcome: SmartCodable {
    
    var code: Int = 0
    var msg: String = ""
    var data: YHFamilyMemberGroupInfo?

    enum CodingKeys: String, CodingKey {
        case code = "code"
        case msg = "msg"
        case data = "data"
    }
    
    required init() {
        
    }
}

// MARK: - 家庭成员 YHData
class YHFamilyMemberGroupInfo: SmartCodable {
    
    var father: YHFamilyMember?
    var mother: YHFamilyMember?
    var spouse: YHFamilyMember?
    var child: [YHFamilyMember]?
    var brother: [YHFamilyMember]?

    enum CodingKeys: String, CodingKey {
        case father = "father"
        case mother = "mother"
        case spouse = "spouse"
        case brother = "brother"
        case child = "child"
    }

    required init() {
        
    }
}

enum YHFamilyMemberType: Int {
    // 与后台relation字段一致
    // 1父亲  2母亲 3配偶 4子女 5兄弟姐妹
    case none = 0
    case father = 1
    case mother = 2
    case spouse = 3
    case child = 4
    case brother = 5
}

class YHFamilyMember: SmartCodable, YHFormItemProtocol {
    
    var id: Int = 0
    // 是否已故
    var deceased: Int = 0
    // 主申请人和现配偶是否拥有孩子抚养权
    var custody: Int = 0
    // 国籍
    var nationality: String?
    var orderId: Int = 0
    // 关系 1父亲  2母亲 3配偶 4子女 5兄弟姐妹
    var relation: String?
    // 是否拥有学位
    var hasDegree: String?
    // 学历数组
    var hasDegreeJson: [YHHasDegreeJson]?
    
    // 是否随行至香港
    var follow: Int = 0
    var statement: Int = 0
    // 姓名
    var subsetName: String?
    
    // mark any
//    var subsetNamePinyin: YHSubsetNamePinyin?
    var subsetNamePinyin: SmartAny?
    // 曾用名
    var usedName: String?
    // 出生日期
    var birthday: String?
    // 出生国家地区 (国内/国外)
    var birthPlaceAboard: Int = 0
    
    // 出生地址信息
    var birthPlace: YHAddress?
    
    // 职业
    var occupation: String?
    // 职业名称
    var occupationName: String?
    
    // mark any
    var operatorAme: SmartAny?
    // 香港身份证号
    var hkIdentityCard: String?
    // 性别 男：1 女：2
    var sex: Int = 0
    // 婚姻状况
    var married: String?
    // 是否与主申请人同住
    var nows: Int = 0
    // 居住地址
    var address: YHAddress?
    
    // 香港身份证信息
    var childHasHkId: YHChildStepchildClass?
    // 是否前次婚姻子女
    var childStepchild: YHChildStepchildClass?
    // 在港信息
    var childInHk: YHChildStepchildClass?
    // 证件数组 (中国身份证、香港身份证、护照等)
    var certificates: YHCertificates?

    // mark any
    var countryIdentity: SmartAny?
    
    // 在港身份
    var hkIdentity: String?
    var hkIdentityOther: String?
    // 现居住国家
    var liveCountry: String?
    // 是否要办理港澳通行证
    var isHandled: Int = 0
    var finishFollow: Int = 0
    var step: Int = 0
    // 未填写项数
    var notFillNum: Int = 0
    // 是否在海外居住满1年及以上
    var isLiveOverseaYear: Int = 0
    // 婚前姓氏
    var surname: String?
    
    enum CodingKeys: String, CodingKey {
        case id = "id"
        case deceased = "deceased"
        case custody = "custody"
        case nationality = "nationality"
        case orderId = "order_id"
        case relation = "relation"
        case hasDegree = "has_degree"
        case hasDegreeJson = "has_degree_json"
        case follow = "follow"
        case statement = "statement"
        case subsetName = "subset_name"
        case subsetNamePinyin = "subset_name_pinyin"
        case usedName = "used_name"
        case birthday = "birthday"
        case birthPlaceAboard = "birth_place_aboard"
        case birthPlace = "birth_place"
        case occupation = "occupation"
        case occupationName = "occupation_name"
        case operatorAme = "operator_ame"
        case childHasHkId = "child_has_hk_id"
        case hkIdentityCard = "hk_identity_card"
        case sex = "sex"
        case married = "married"
        case nows = "nows"
        case address = "address"
        case childStepchild = "child_stepchild"
        case childInHk = "child_in_hk"
        case countryIdentity = "country_identity"
        case hkIdentity = "hk_identity"
        case hkIdentityOther = "hk_identity_other"
        case liveCountry = "live_country"
        case isHandled = "is_handled"
        case finishFollow = "finish_follow"
        case step = "step"
        case notFillNum = "not_fill_num"
        case isLiveOverseaYear = "is_live_oversea_year"
        case surname = "surname"
        case certificates = "certificates"
    }
    
    required init() {
        
    }
    
    // 是否随行
    func isFollow() -> Bool {
        return follow == 1
    }
    
    func setFollow(_ value:Bool) {
        self.follow = (value ? 1 : 0)
    }
    
    // 是否要办理港澳通行证
    func isNeedHandleHKPassPort() -> Bool {
        return  self.isHandled == 1
    }
    
    func setNeedHandleHKPassPort(_ value:Bool) {
        self.isHandled = (value ? 1 : 0)
    }
    
    // 是否与主申请人同住
    func isLiveTother() -> Bool {
        return self.nows == 1
    }
    
    func setLiveTother(_ value:Bool) {
        self.nows = (value ? 1 : 0)
    }
    
    // 是否在海外居住满1年及以上
    func isOverSeasOver1Year() -> Bool {
        return self.isLiveOverseaYear == 1
    }
    
    func setOverSearsOver1Year(_ value:Bool) {
        self.isLiveOverseaYear = value ? 1 : 0
    }
    
    // 是否出生国外
    func isBirthOverSeas() -> Bool {
        return self.birthPlaceAboard == 1
    }
    
    func setBirthOverSeas(_ value:Bool) {
        return self.birthPlaceAboard = value ? 1 : 0
    }
    
    // 目前是否在港
    func isNowInHK() -> Bool {
        if let inHK = self.childInHk {
            if inHK.has == "Y" {
                return true
            } else if inHK.has == "N" {
                return false
            }
        }
        return false
    }
    
    func setNowIsInHK(_ value:Bool) {
        self.childInHk?.has = value ?  "Y" : "N"
    }
    
    // 是否办理过香港身份证
    func isHaveHKIdentityCard() -> Bool {
        if let hasHkId = self.childHasHkId {
            if hasHkId.has == "Y" {
                return true
            } else if hasHkId.has == "N" {
                return false
            }
        }
        return false
    }
    
    func setHaveHKIdentityCard(_ value:Bool) {
        self.childHasHkId?.has = value ?  "Y" : "N"
    }
    
    // 是否拥有学位
    func haveDegreeName() -> String? {
        if hasDegree == "1" {
            return "有学位"
        } else {
            return "无学位"
        }
    }
    
    func isHaveDegree() ->Bool {
        return self.hasDegree == "1"
    }
    
    func setHaveDegree(_ val:Bool) {
        self.hasDegree = val ? "1" : "0"
    }
    
    // 是否已故
    func isDead() -> Bool {
        return self.deceased == 1
    }
    
    func setIsDead(_ val:Bool) {
        self.deceased = (val ? 1 : 0)
    }
    
    // 是否在职
    func isNowHaveJob() -> Bool {
        return self.occupation == "在职"
    }
    
    // 主申请人和现配偶是否拥有孩子抚养权
    func isOwnCustody() -> Bool {
        return self.custody == 1
    }
    
    func setOwnCustody(_ val:Bool) {
        self.custody = val ? 1 : 0
    }
    
    // 是否是男性
    func isMale() -> Bool {
      return  self.sex == 1
    }
    
    func setMale(_ val: Bool) {
        self.sex = (val ? 1 : 2)
    }
    
    func sexName() -> String? {
        if self.sex == 1 {
            return "男".local
        } else if self.sex == 2 {
            return "女".local
        }
        return ""
    }
    
    // 是否前一次婚姻子女
    func isPreviousMarriageChild() -> Bool {
       return self.childStepchild?.has == "Y"
    }
    
    func setPreviousMarriageChild(_ val: Bool) {
        self.childStepchild?.has = (val ? "Y" : "N")
    }
    
    
    var relationType:YHFamilyMemberType {
        
        if relation == "1" {
            return .father
        } else if relation == "2" {
            return .mother
        } else if relation == "3" {
            return .spouse
        } else if relation == "4" {
            return .child
        } else if relation == "5" {
            return .brother
        }
        
        return .none
    }
    
    // YHFamilyMemberProtocol
    func getTitle() -> String {
        // 1父亲  2 母亲 3 配偶 4子女 5兄弟姐妹
        if let relation = relation {
            if relation == "1" {
                return "父亲".local
            } else if relation == "2" {
                return "母亲".local
            } else if relation == "3" {
                return "配偶".local
            } else if relation == "4" {
                return "子女".local
            } else if relation == "5" {
                return "兄弟姐妹".local
            }
        }
        return ""
    }
    
    func getSubTitle() -> String {
        return ""
    }
}

// MARK: - 居住地址信息 YHAddress
class YHAddress: SmartCodable {
    
    var area: [String]?
    var country: String?
    var details: String?
    var foreign: String?

    enum CodingKeys: String, CodingKey {
        case area = "area"
        case country = "country"
        case details = "details"
        case foreign = "foreign"
    }

    required init() {
        
    }
}

// MARK: - YHChildStepchildClass
class YHChildStepchildClass: SmartCodable {
    var has: String?
    var info: String?

    enum CodingKeys: String, CodingKey {
        case has = "has"
        case info = "info"
    }

    required init() {
        
    }
}

// MARK: - YHSubsetNamePinyin
class YHSubsetNamePinyin: SmartCodable {
    var familyName: String?
    var givenName: String?

    enum CodingKeys: String, CodingKey {
        case familyName = "family_name"
        case givenName = "given_name"
    }

    required init() {
        
    }
}

// MARK: - YHCertificates
class YHCertificates: SmartCodable {
    
    var cnIdentityCard: YHCNIdentityCard?
    var passport: YHCNIdentityCard?
    var hkMacaoPass: YHCNIdentityCard?

    enum CodingKeys: String, CodingKey {
        case cnIdentityCard = "cnIdentityCard"
        case passport = "passport"
        case hkMacaoPass = "hkMacaoPass"
    }
        
    required init() {
        
    }
    
    init(cnIdentityCard: YHCNIdentityCard?, passport: YHCNIdentityCard?, hkMacaoPass: YHCNIdentityCard?) {
        self.cnIdentityCard = cnIdentityCard
        self.passport = passport
        self.hkMacaoPass = hkMacaoPass
    }
}

// MARK: - YHCNIdentityCard
class YHCNIdentityCard: SmartCodable {
    
    var id: Int = 0
    var number: String?
    var issueAt: String?
    var issueDateStartAt: String?
    var issueDateEndAt: String?
    var imgFront: String?
    var imgBack: String?
    var passportType: Int = 0

    enum CodingKeys: String, CodingKey {
        case id = "id"
        case number = "number"
        case issueAt = "issue_at"
        case issueDateStartAt = "issue_date_start_at"
        case issueDateEndAt = "issue_date_end_at"
        case imgFront = "img_front"
        case imgBack = "img_back"
        case passportType = "passport_type"
    }
    
    required init() {
        
    }
    
    init(id:Int, number: String?, issueAt:String?,  issueDateStartAt: String?, imgFront: String?, imgBack: String?, passportType: Int) {
        self.id = id
        self.number = number
        self.issueAt = issueAt
        self.issueDateStartAt = issueDateStartAt
        self.imgFront = imgFront
        self.imgBack = imgBack
        self.passportType = passportType
    }
}

// MARK: - YHHasDegreeJson
class YHHasDegreeJson: SmartCodable {
    
    
    var degree: String?
    var address: String?

    enum CodingKeys: String, CodingKey {
        case degree = "degree"
        case address = "address"
    }

    required init() {
        
    }
}
