package com.app.simplitend.customsviews;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.daimajia.androidanimations.library.Techniques;
import com.daimajia.androidanimations.library.YoYo;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.app.simplitend.R;

public class HomeBottomNav extends FrameLayout {

    // views
    private FloatingActionButton dashBtnBig, patientBig, chatsBig;
    private LinearLayout dashSmall, patientSmall, chatsSmall;

    private TextView chatWithTxt, myPatientTxt;

    // fields
    private MenuItem selected_item = MenuItem.DASHBOARD;

    private OnBottomNavItemSelectListener itemSelectListener;

    // constructors
    public HomeBottomNav(@NonNull Context context) {
        super(context);
        initViews();
    }

    public HomeBottomNav(@NonNull Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initViews();
    }

    public HomeBottomNav(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initViews();
    }

    private void initViews(){
        View view = inflate(getContext(), R.layout.home_bottom_nav, this);

        dashBtnBig = view.findViewById(R.id.dadh_big);
        dashSmall = view.findViewById(R.id.dash_small);

        patientBig = view.findViewById(R.id.patient_big);
        patientSmall = view.findViewById(R.id.patient_small);

        chatsBig = view.findViewById(R.id.chat_big);
        chatsSmall = view.findViewById(R.id.chats_small);

        chatWithTxt = view.findViewById(R.id.chat_with);
        myPatientTxt = view.findViewById(R.id.my_patient);

        clickEvents();
    }

    private void clickEvents() {
        dashSmall.setOnClickListener(v -> {
            selectMenuItem(MenuItem.DASHBOARD);
            if (itemSelectListener != null) {
                itemSelectListener.onBottomNavItemSelected(MenuItem.DASHBOARD);
            }
        });

        chatsSmall.setOnClickListener(v -> {
            selectMenuItem(MenuItem.CHATS);
            if (itemSelectListener != null) {
                itemSelectListener.onBottomNavItemSelected(MenuItem.CHATS);
            }
        });

        patientSmall.setOnClickListener(v -> {
            selectMenuItem(MenuItem.MY_PATIENT);
            if (itemSelectListener != null) {
                itemSelectListener.onBottomNavItemSelected(MenuItem.MY_PATIENT);
            }
        });
    }

    public void setItemSelectListener(OnBottomNavItemSelectListener itemSelectListener) {
        this.itemSelectListener = itemSelectListener;
    }

    public void setMyPatient(String myPatient){
        if (myPatient != null && myPatientTxt != null){
            myPatientTxt.setText(myPatient);
        }
    }

    public void setChatWithTxt(String chatWith){
        if (chatWith != null && chatWithTxt != null){
            chatWithTxt.setText(chatWith);
        }
    }

    public void selectMenuItem(MenuItem menuitem){

        clearItemSelection(this.selected_item);

        this.selected_item = menuitem;

        FloatingActionButton selectedBig = null;
        LinearLayout selectedSmall = null;

        switch (menuitem){
            case DASHBOARD:
                selectedBig = dashBtnBig;
                selectedSmall = dashSmall;
                break;
            case CHATS:
                selectedBig = chatsBig;
                selectedSmall = chatsSmall;
                break;
            case MY_PATIENT:
                selectedBig = patientBig;
                selectedSmall = patientSmall;
                break;
        }

        final FloatingActionButton final_selectedBig = selectedBig;
        final LinearLayout final_selectedSmall = selectedSmall;

        if (final_selectedBig != null && final_selectedSmall != null){

            YoYo.with(Techniques.SlideOutUp)
                    .duration(100)
                    .onEnd(animator -> {
                        final_selectedBig.setVisibility(VISIBLE);
                        final_selectedSmall.setVisibility(GONE);

                        YoYo.with(Techniques.SlideInUp)
                                .duration(100)
                                .playOn(final_selectedBig);
                    })
                    .playOn(final_selectedSmall);

        }

    }

    private void clearItemSelection(MenuItem selected_item) {
        FloatingActionButton selectedBig = null;
        LinearLayout selectedSmall = null;

        switch (selected_item){
            case DASHBOARD:
                selectedBig = dashBtnBig;
                selectedSmall = dashSmall;
                break;
            case CHATS:
                selectedBig = chatsBig;
                selectedSmall = chatsSmall;
                break;
            case MY_PATIENT:
                selectedBig = patientBig;
                selectedSmall = patientSmall;
                break;
        }

        final FloatingActionButton final_selectedBig = selectedBig;
        final LinearLayout final_selectedSmall = selectedSmall;

        if (final_selectedBig != null && final_selectedSmall != null){

            YoYo.with(Techniques.SlideOutDown)
                    .duration(100)
                    .onEnd(animator -> {
                        final_selectedBig.setVisibility(GONE);
                        final_selectedSmall.setVisibility(VISIBLE);

                        YoYo.with(Techniques.SlideInDown)
                                .duration(100)
                                .playOn(final_selectedSmall);
                    })
                    .playOn(final_selectedBig);

        }

    }

    // callback interfaces
    @FunctionalInterface
    public interface OnBottomNavItemSelectListener{
        void onBottomNavItemSelected(MenuItem selectedItem);
    }

}
