//
//  PageControlVIew.swift
//  MobilePortal
//
//  Created by caiyang on 2020/5/11.
//  Copyright © 2020 yin chen. All rights reserved.
//

import UIKit

class PageControlVIew: UIView {
//    UIImage.init(named: "pageControl_select_image")
//    UIImage.init(named: "pageControl_image")
    // pageControl_left_select_image
//    pageControl_right_select_image
    var imageArray = NSMutableArray()
    var selsetIndex = Int()
    var num = Int()
    var intervalW:CGFloat = 1.33
    var itemW:CGFloat = 13.33
    override init(frame: CGRect) {
        super.init(frame: frame)
//        titleLabel.frame = CGRect(x:30, y: 0, width:200 , height:self.height )
//        banjiBtn.frame = CGRect(x:SCREEN_WHIDTH()-60, y: 0, width:30 , height:self.height )
    }
    func addItemImage(){
        let b:CGFloat = intervalW+itemW
//        self.removeFromSuperview()
//        self.imageArray = []
        let chilrenviews = self.subviews
        for chilren in chilrenviews {
            chilren.removeFromSuperview()
        }
        if num == 1 {
            addOne()
        } else if num == 2 {
            addTwo()
        } else if num > 2 {
            for index in 0..<num {
                let itemView = self.getItemView(index: 0)
                itemView.frame = CGRect(x:b*CGFloat(index), y: 0, width:itemW , height:self.height )
                itemView.image = UIImage.init(named: "pageControl_image")
                self.imageArray.add(itemView)
            }
        }
    }
    /// 一个
    //添加
    func addOne(){
        let itemView = self.getItemView(index: 0)
        itemView.image = UIImage.init(named: "pageControl_image")
        self.imageArray.add(itemView)
    }
    /// 两个
    func addTwo(){
        let leftView = self.getItemView(index: 0)
        leftView.image = UIImage.init(named: "pageControl_left_image")
        self.imageArray.add(leftView)
        let rithtView = self.getItemView(index: 0)
        rithtView.image = UIImage.init(named: "pageControl_right_image")
        self.imageArray.add(rithtView)
    }
    /// 两个以上
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    func getItemView(index:Int)->UIImageView{
        let pageConimageView = UIImageView.init()
        pageConimageView.tag = 100+index
        self.addSubview(pageConimageView)
        return pageConimageView
    }
    func switchImage(selectIndex:Int) {
        for index in 0..<self.imageArray.count {
            let itemImage:UIImageView = self.imageArray[index] as! UIImageView
            itemImage.image = UIImage.init(named: "pageControl_image")
            if num > 2 && index == 0{
                itemImage.image = UIImage.init(named: "pageControl_left_image")
            }
            if num > 2 && index == num-1{
                itemImage.image = UIImage.init(named: "pageControl_right_image")
            }
            if index == selectIndex {
                if num > 2 && index == 0{
                    itemImage.image = UIImage.init(named: "pageControl_left_select_image")
                } else if num > 2 && index == num-1{
                    itemImage.image = UIImage.init(named: "pageControl_right_select_image")
                }else{
                    itemImage.image = UIImage.init(named: "pageControl_select_image")
                }
            }
        }
    }
}
