package com.ym.library.widget;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.core.graphics.drawable.DrawableCompat;

import com.ym.library.R;
import com.ym.library.utils.RxBus;
import com.ym.modulecommon.AppliContext;

public class NavigationBottomTab extends RelativeLayout {

    public int mId;

    private boolean isSelected = false;

    private int mTitleColor;
    private String mTitle;
    private int mTitleSelectColor;

    private Drawable mCompactIcon;
    private Drawable mCompactSelectIcon;
    private Drawable mTaskIcon;

    private Context mContext;

    private TextView labelView;
    private TextView numberView;
    private ImageView iconView;
    private ImageView dropView;
    private boolean mIsShowDrop;
    private Animation scaleAnimation;

    public NavigationBottomTab(Context context, String title, Drawable icon, Drawable mSelectIcon, Drawable mTaskIcon, int titleColor, int selectTitleClot,
                               int id, boolean isShowDrop) {
        super(context);
        mContext = context;
        mTitle = title;
        mCompactIcon = DrawableCompat.wrap(icon);
        mCompactSelectIcon = DrawableCompat.wrap(mSelectIcon);
        this.mTaskIcon = mTaskIcon;
        mId = id;
        mTitleColor = titleColor;
        mTitleSelectColor = selectTitleClot;
        mIsShowDrop = isShowDrop;
        initView();
    }

    public NavigationBottomTab(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public NavigationBottomTab(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView();
    }

    private void initView() {
        scaleAnimation = AnimationUtils.loadAnimation(AppliContext.get(), R.anim.anim_bottom_button);

        LinearLayout.LayoutParams parentLayoutParam = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        parentLayoutParam.weight = 1;
        parentLayoutParam.gravity = Gravity.CENTER_VERTICAL;
        setLayoutParams(parentLayoutParam);

        View view = LayoutInflater.from(mContext).inflate(R.layout.item_navigation_bottom_tab, this, false);
        iconView = view.findViewById(R.id.id_navigation_tab_icon);
        numberView = view.findViewById(R.id.tv_bottom_number);
        dropView = view.findViewById(R.id.id_item_bottom_tab_drop);
        dropView.setVisibility(mIsShowDrop ? VISIBLE : GONE);
        labelView = view.findViewById(R.id.id_item_bottom_tab_text);
        labelView.setText(mTitle);

        unSelect();
        addView(view);
        setId(mId);
    }

    public void setBottomNum(int num){
        if (num == 0){
            numberView.setVisibility(GONE);
        }else {
            numberView.setVisibility(VISIBLE);
            numberView.setText(""+num);
        }
    }

    private int rotation = 0;

    public void select() {
        if (isSelected) {
            if (mId == 2) {
                RxBus.get().post("refresh_news", 0);
//                rotation += 90;
//                iconView.setRotation(rotation);
            }
        }

        isSelected = true;
        iconView.setImageDrawable(mCompactSelectIcon);
//        iconView.setBackgroundDrawable(mCompactSelectIcon);
        iconView.startAnimation(scaleAnimation);
        if (mTitleSelectColor != 0) {
            if (mId != 13) {
                labelView.setTextColor(getContext().getResources().getColor(mTitleSelectColor));
            }
        }
    }

    public void unSelect() {
        isSelected = false;
        iconView.setImageDrawable(mCompactIcon);
        if (mTitleColor != 0) {
            labelView.setTextColor(getContext().getResources().getColor(mTitleColor));
        }
    }

    public void taskUnselect() {
        isSelected = false;
        iconView.setImageDrawable(mTaskIcon);
        if (mTitleColor != 0) {
            labelView.setTextColor(Color.parseColor("#323334"));
        }
    }

    public void setNavigationIcon(String codeDay) {
//        iconView.setImageResource(WeatherDrawableUtils.INSTANCE.getNavigationIcon(codeDay, mId));
    }

    public void setSelectColor(int color) {
        labelView.setTextColor(color);
    }

    public void setDropShowOrHint(boolean isClockIn) {
        if (dropView != null) {
            if (isClockIn) {
                dropView.setVisibility(VISIBLE);
            } else {
                dropView.setVisibility(GONE);
            }
        }
    }
}
