//
//  MineMoreCollectionViewCell.swift
//  MobilePortal
//
//  Created by xiejiangbo on 2019/3/26.
//  Copyright © 2019 yin chen. All rights reserved.
//

import UIKit

class MineMoreCollectionViewCell: BaseCollectionViewCell {
    
    var block:passParameterBlock!
    var modelHttp = MineMoreCollectionViewCellModel()
    var text :String!
    
    var editStatusType:EditStatusType = .StartEditing {
        didSet {
            if editStatusType == .FinishEditing {
                operatingView.isHidden = true
                bagView.isHidden = true
                installationView.isHidden = false
            }else{
                operatingView.isHidden = false
                bagView.isHidden = false
                installationView.isHidden = true
            }
        }
    }
    var operationType:OperationType = .DeleteType {
        didSet {
            if operationType == .AddType {
                operatingView.setBackgroundImage(UIImage.init(named: "moreDdd_image"), for: .normal)
            }else{
                operatingView.setBackgroundImage(UIImage.init(named: "moreDele_image"), for: .normal)
            }
        }
    }
    var indexPath:IndexPath =  IndexPath(){
        didSet {
            if indexPath.section == 0 {
                operatingView.setBackgroundImage(UIImage.init(named: "moreDele_image"), for: .normal)
            }else{
                operatingView.setBackgroundImage(UIImage.init(named: "moreDdd_image"), for: .normal)
            }
        }
    }
    
    
    lazy var titleImage: UIImageView = {
        let titleImage = UIImageView.init()
        titleImage.contentMode = .scaleAspectFit
        titleImage.backgroundColor = UIColor.clear
        self .addSubview(titleImage)
        return titleImage
    }()
    lazy var installationView: UIImageView = {
        let installationView = UIImageView.init()
        installationView.isHidden = true
        self .addSubview(installationView)
        return installationView
    }()
    
    lazy var titleLable: UILabel = {
        let titleLable = UILabel.init()
        titleLable.textAlignment = .center
        titleLable.font = UIFont.systemFont(ofSize: 14*WHIDTH_RATIO())
        titleLable.numberOfLines = 0
        titleLable.backgroundColor = UIColor.clear
        titleLable.lineBreakMode = .byCharWrapping
        titleLable.textColor = blackTitleColor
        self.addSubview(titleLable)
        return titleLable
    }()
    lazy var operatingView: UIButton = {
        let operatingBtn = UIButton.init(type: .custom)
        operatingBtn.addTarget(self, action: #selector(operatingAction), for: .touchUpInside)
        operatingBtn.isHidden = true
        self.addSubview(operatingBtn)
        return operatingBtn
    }()
    lazy var bagView: UIImageView = {
        let bagView = UIImageView.init()
        bagView.backgroundColor = moreClassColor
        bagView.isHidden = true
        self .addSubview(bagView)
        return bagView
    }()
    lazy var baojiBtn: UIButton = {
        let baojiBtn = UIButton.init(type: .custom)
        baojiBtn.setTitleColor(UIColor.white, for: .normal)
        baojiBtn.titleLabel?.font = UIFont.systemFont(ofSize: 10)
        baojiBtn.setBackgroundImage(UIImage.init(named: "biaoji_image"), for: .normal)
        baojiBtn.isHidden = true
        self.addSubview(baojiBtn)
        return baojiBtn
    }()
    
    override func awakeFromNib() {
        super.awakeFromNib()
        self.backgroundColor = UIColor.white
    }
    override func configurateTheCell(info:BaseCollectionViewCellModel){
        
        self.modelHttp = info as! MineMoreCollectionViewCellModel
        self.block = self.modelHttp.block
        
        bagView.frame = CGRect(x:2.5*WHIDTH_RATIO(), y: 2.5*HEIGHT_RATIO(), width:self.width-5*WHIDTH_RATIO() , height:self.width-5*WHIDTH_RATIO() )
        
//        let titleImageW = self.width-73*WHIDTH_RATIO()
        let titleImageW = 26*WHIDTH_RATIO()
        titleImage.frame = CGRect(x:(self.width-titleImageW)/2, y: 10*WHIDTH_RATIO(), width:titleImageW , height:titleImageW )
        titleLable.frame = CGRect(x:5*WHIDTH_RATIO(), y: titleImage.originY+titleImage.height+5*HEIGHT_RATIO(), width:self.width-10*WHIDTH_RATIO() , height:getTextHeigh(textStr: modelHttp.titleValue!, font: UIFont.systemFont(ofSize: 14*WHIDTH_RATIO()), width: self.width-10*WHIDTH_RATIO()) )
        
        let baojiBtnWidth = self.titleImage.width/2.2
        operatingView.frame = CGRect(x:self.titleImage.originX+self.titleImage.width-baojiBtnWidth, y: self.titleImage.originY/2, width:baojiBtnWidth , height:baojiBtnWidth)
        operationType = modelHttp.operationType
        if modelHttp.operationType == .AddType {
            operatingView.setBackgroundImage(UIImage.init(named: "moreDdd_image"), for: .normal)
        }else{
            operatingView.setBackgroundImage(UIImage.init(named: "moreDele_image"), for: .normal)
        }
        titleImage.sd_setImage(with: URL.init(string: modelHttp.imageName!), placeholderImage: UIImage.init(named: "center_image"))
        //titleImage.image = UIImage.init(named: "agentServices_image")
        titleLable.text = modelHttp.titleValue
        
        baojiBtn.frame = CGRect(x:self.titleImage.originX+self.titleImage.width-baojiBtnWidth*2, y: self.titleImage.originY/2, width:baojiBtnWidth , height:baojiBtnWidth)
        if self.modelHttp.biaojiNum == nil || self.modelHttp.biaojiNum?.count == 0 || self.modelHttp.biaojiNum == "0"{
            baojiBtn.isHidden = true
        }else{
            baojiBtn.isHidden = false
            baojiBtn.setTitle(self.modelHttp.biaojiNum, for: .normal)
            // baojiBtn.setTitle("2", for: .normal)
        }
        if self.modelHttp.applicationType == .MyApplicationType && self.modelHttp.comments?.count == 0{
            installationView.frame = CGRect(x:self.width-10*WHIDTH_RATIO()-baojiBtnWidth, y: 5*HEIGHT_RATIO(), width:baojiBtnWidth , height:baojiBtnWidth )
            if modelHttp.isInstallation == false{
                installationView.image = UIImage.init(named: "Yes_installation_image")
            }else{
                installationView.image = UIImage.init(named: "No_installation_image")
            }
        }else if self.modelHttp.applicationType == .MyApplicationType && self.modelHttp.comments?.count != 0{
            installationView.isHidden = true
        }
        else{
            installationView.isHidden = true
        }
    }
    
    @objc func operatingAction(){
        //self.block(indexPath as AnyObject)
    }
}
class MineMoreCollectionViewCellModel: BaseCollectionViewCellModel {
    
    var titleValue:String?
    var imageName:String?
    var biaojiNum:String?
    var block:passParameterBlock!
    var url:String?
    var operationType:OperationType = .DeleteType
    var applicationType:ApplicationType = .MyApplicationType
    var id:String?
    var title:String?
    var status:String?
    var createon:String?
    var createby:String?
    var sort:String?
    var pid:String?
    var tags:String?
    var author:String?
    var createbyname:String?
    var updatebyname:String?
    var image:String?
    var homeshow:String?
    var comments:String?
    var docname:String?
    var type:String?
    var foldertype:String?
    var kgimage:String?
    var listimage:String?
    var updateon:String?
    var openUrl:String?
    var appid:String?
    var isInstallation = true
}
