package ch.pizzalemon.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import androidx.appcompat.widget.Toolbar;

import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import ch.pizzalemon.android.R;
import ch.pizzalemon.android.activity.BaseActivity;

/**
 * Created by cimenmus on 18/09/2017.
 */

public class AppToolbar extends Toolbar {

    private View rootView;
    private TextView toolbarTitleTextView;
    private ImageView hamburgerIcon, backIcon, closeIcon;
    private boolean showHamburgerIcon, showBackIcon, showCloseIcon;
    private String title;
    private int toolbarBackgroundColor, titleTextColor;

    /*
    public PizzalinkToolbar(Toolbar toolbar){
        this.toolbar = toolbar;
        initViews();
        toolbar.setBackgroundColor(ContextCompat.getColor(BaseActivity.currentActivity, android.R.color.transparent));
    }
    */

    public AppToolbar(Context context) {
        super(context);
        init(context);
    }

    public AppToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.AppToolbar, 0, 0);
        try {
            showHamburgerIcon = a.getBoolean(R.styleable.AppToolbar_showHamburgerMenuIcon, false);
            showCloseIcon = a.getBoolean(R.styleable.AppToolbar_showCloseIcon, false);
            showBackIcon = a.getBoolean(R.styleable.AppToolbar_showBackIcon, false);
            title = a.getString(R.styleable.AppToolbar_title);
            /*
            toolbarBackgroundColor = a.getColor(R.styleable.PizzalinkToolbar_toolbarBackgroundColor,
                    ContextCompat.getColor(BaseActivity.currentActivity, R.color.colorPrimary));
            titleTextColor = a.getColor(R.styleable.PizzalinkToolbar_toolbarTitleTextColor,
                    ContextCompat.getColor(BaseActivity.currentActivity, R.color.white));
                    */

        } finally {
            a.recycle();
        }
        init(context);
    }

    private void init(Context context){
        rootView = inflate(context, R.layout.layout_pizzalink_toolbar, this);
        this.setPadding(0, 0, 0, 0);
        this.setContentInsetsAbsolute(0, 0);
        toolbarTitleTextView = (TextView) rootView.findViewById(R.id.toolbarTitleTextView);
        //this.setBackgroundResource(toolbarBackgroundColor);
        //toolbarTitleTextView.setTextColor(titleTextColor);
        hamburgerIcon = (ImageView) rootView.findViewById(R.id.hamburgerIcon);
        closeIcon = (ImageView) rootView.findViewById(R.id.closeIcon);
        backIcon = (ImageView) rootView.findViewById(R.id.backIcon);
        if(showHamburgerIcon)
            hamburgerIcon.setVisibility(VISIBLE);
        if(showBackIcon)
            backIcon.setVisibility(VISIBLE);
        if(showCloseIcon)
            closeIcon.setVisibility(VISIBLE);
        backIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivity.currentActivity.onBackPressed();
            }
        });
        closeIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                BaseActivity.currentActivity.onBackPressed();
            }
        });
        if(title != null) {
            toolbarTitleTextView.setText(title);
            toolbarTitleTextView.setVisibility(VISIBLE);
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
            FrameLayout spacerViewForStatusBar = rootView.findViewById(R.id.spacerViewForStatusBar);
            spacerViewForStatusBar.setVisibility(View.VISIBLE);
        }
    }

    public Toolbar getToolbar() {
        return (Toolbar) rootView;
    }

    public void setTitle(String title){
        rootView.setVisibility(View.VISIBLE);
        toolbarTitleTextView.setVisibility(View.VISIBLE);
        toolbarTitleTextView.setText(title);
    }

    public void setHamburgerIconVisibility(boolean show) {
        if(show)
            hamburgerIcon.setVisibility(VISIBLE);
        else
            hamburgerIcon.setVisibility(GONE);
    }

    public ImageView getHamburgerIcon() {
        return hamburgerIcon;
    }

    public void setBackIconVisibility(boolean isVisible){
        showBackIcon = isVisible;
        if(isVisible){
            backIcon.setVisibility(VISIBLE);
        }
        else {
            backIcon.setVisibility(GONE);
        }
    }
}
