package com.example.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.List;

public class SwitchLayout extends LinearLayout implements View.OnClickListener {

    //子view
    private ImageView imageViewLeft;
    private ImageView imageViewRight;
    private TextView textView;
    //数据列表
    private List<String> data;
    private int selectedIndex = -1;
    //点击事件
    private ClickEvent clickEvent;
    //默认字体大小
    private int defaultTextSize = 15;
    private int defaultImageSize = 60;

    public SwitchLayout(Context context, AttributeSet attributeSet, int def){
        super(context,attributeSet,def);
        init(context,attributeSet);
    }

    public SwitchLayout(Context context,AttributeSet attributeSet){
        this(context,attributeSet,0);
    }

    public SwitchLayout(Context context){
        this(context,null);
    }

    private void init(Context context,AttributeSet attributeSet){
        //获取属性
        TypedArray typedArray = context.obtainStyledAttributes(attributeSet,R.styleable.SwitchLayout);
        int imageSize = (int)typedArray.getDimension(R.styleable.SwitchLayout_image_size,defaultImageSize);
        float textSize = typedArray.getDimension(R.styleable.SwitchLayout_text_size,defaultTextSize);
        //内容居中
        LayoutParams layoutParams = new LinearLayout.LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        this.setLayoutParams(layoutParams);
        //左箭头
        imageViewLeft = new ImageView(context);
        imageViewLeft.setImageResource(R.drawable.ic_left_arrow);
        imageViewLeft.setOnClickListener(this);
        ViewGroup.LayoutParams layoutParamsImage = new LayoutParams(imageSize,imageSize);
        imageViewLeft.setLayoutParams(layoutParamsImage);
        this.addView(imageViewLeft);
        //被选中项
        textView = new TextView(context);
        textView.setText("<empty>");
        ViewGroup.LayoutParams layoutParams1 = new LayoutParams
                (ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
        textView.setLayoutParams(layoutParams1);
        textView.setGravity(View.TEXT_ALIGNMENT_CENTER);
        textView.setTextSize(textSize);
        this.addView(textView);
        //右箭头
        imageViewRight = new ImageView(context);
        imageViewRight.setImageResource(R.drawable.ic_right_arrow);
        imageViewRight.setOnClickListener(this);
        imageViewRight.setLayoutParams(layoutParamsImage);
        this.addView(imageViewRight);
    }

    /**
     * 设置数据
     * @param data
     */
    public void build(List<String> data,int selectedIndex,ClickEvent clickEvent){
        this.data = data;
        this.clickEvent = clickEvent;
        changeSelected(selectedIndex);
    }

    /**
     * 修改数据列表
     */
    private void changeSelected(int selectedIndex){
        this.selectedIndex = selectedIndex;
        textView.setText(data.get(selectedIndex));
        clickEvent.onClick();
    }

    @Override
    public void onClick(View v) {
        if(v.equals(imageViewLeft)){
            if(selectedIndex == 0){
                selectedIndex = data.size() - 1;
            }else {
                selectedIndex = selectedIndex - 1;
            }
        }else if(v.equals(imageViewRight)){
            if(selectedIndex == data.size() - 1){
                selectedIndex = 0;
            }else {
                selectedIndex = selectedIndex + 1;
            }
        }
        //修改UI,执行点击事件
        changeSelected(selectedIndex);
    }


    /**
     * 点击事件
     */
    public interface ClickEvent{
        public void onClick();
    }
}