package com.frame.rv.widget;

import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.frame.rv.ModuleEventUtil;
import com.frame.rv.OnModuleEventListener;
import com.frame.rv.R;

public class BaseTabLayout extends FrameLayout {
    public BaseTabLayout(Context context) {
        super(context);
        init(context);

    }

    public BaseTabLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);

    }

    public BaseTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    ImageView ivHome;
    TextView tvHome;
    ImageView ivTravel;
    TextView tvTravel;
    ImageView ivCamp;
    TextView tvCamp;
    ImageView ivFound;
    TextView tvFound;
    ImageView ivMine;
    TextView tvMine;


    private View.OnClickListener listener = new OnClickListener() {
        @Override
        public void onClick(View v) {
            int i = v.getId();
            if (i == R.id.rl_home) {
                if (ModuleEventUtil.getOnModuleEventListener() != null) {
                    ModuleEventUtil.getOnModuleEventListener().onChanged(OnModuleEventListener.MODULE_HOME);
                }
            } else if (i == R.id.rl_travel) {

                if (ModuleEventUtil.getOnModuleEventListener() != null) {
                    ModuleEventUtil.getOnModuleEventListener().onChanged(OnModuleEventListener.MODULE_TRAVEL);
                }
            } else if (i == R.id.rl_camp) {
                if (ModuleEventUtil.getOnModuleEventListener() != null) {
                    ModuleEventUtil.getOnModuleEventListener().onChanged(OnModuleEventListener.MODULE_CAMP);
                }
            } else if (i == R.id.rl_found) {
                if (ModuleEventUtil.getOnModuleEventListener() != null) {
                    ModuleEventUtil.getOnModuleEventListener().onChanged(OnModuleEventListener.MODULE_FIND);
                }
            } else if (i == R.id.rl_mine) {
                if (ModuleEventUtil.getOnModuleEventListener() != null) {
                    ModuleEventUtil.getOnModuleEventListener().onChanged(OnModuleEventListener.MODULE_MINE);
                }
            }
        }
    };


    private void init(Context context) {
        View view = LayoutInflater.from(context).inflate(R.layout.layout_tab, this, true);
        tvHome = view.findViewById(R.id.tv_home);
        tvTravel = view.findViewById(R.id.tv_travel);
        tvCamp = view.findViewById(R.id.tv_camp);
        tvFound = view.findViewById(R.id.tv_found);
        tvMine = view.findViewById(R.id.tv_mine);

        ivHome = view.findViewById(R.id.iv_home);
        ivTravel = view.findViewById(R.id.iv_travel);
        ivCamp = view.findViewById(R.id.iv_camp);
        ivFound = view.findViewById(R.id.iv_found);
        ivMine = view.findViewById(R.id.iv_mine);

        view.findViewById(R.id.rl_home).setOnClickListener(listener);
        view.findViewById(R.id.rl_travel).setOnClickListener(listener);
        view.findViewById(R.id.rl_camp).setOnClickListener(listener);
        view.findViewById(R.id.rl_found).setOnClickListener(listener);
        view.findViewById(R.id.rl_mine).setOnClickListener(listener);

    }

    private int currentTab = OnModuleEventListener.MODULE_HOME;

    public void setCurrent(int tab) {
        currentTab = tab;
        setCurrentTab();
    }


    /**
     * 设置切换
     */
    private void setCurrentTab() {
        setViewStyle(ivHome, tvHome, currentTab == OnModuleEventListener.MODULE_HOME);
        setViewStyle(ivTravel, tvTravel, currentTab == OnModuleEventListener.MODULE_TRAVEL);
        setViewStyle(ivCamp, tvCamp, currentTab == OnModuleEventListener.MODULE_CAMP);
        setViewStyle(ivFound, tvFound, currentTab == OnModuleEventListener.MODULE_FIND);
        setViewStyle(ivMine, tvMine, currentTab == OnModuleEventListener.MODULE_MINE);
    }

    /**
     * 设置view的样式
     */
    private void setViewStyle(ImageView imageView, TextView textView, boolean selected) {
        imageView.setSelected(selected);
        textView.setTextColor(ContextCompat.getColor(getContext(), selected ? R.color.colorH1 : R.color.colorTextH1));
    }


}
