package ch.pizzalink.android.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.Toolbar;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

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

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

public class PizzalinkToolbar extends Toolbar {

    private View rootView;
    private TextView toolbarTitleTextView;
    private ImageView hamburgerIcon, backIcon;
    private boolean showHamburgerIcon, showBackIcon;
    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 PizzalinkToolbar(Context context) {
        super(context);
        init(context);
    }

    public PizzalinkToolbar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PizzalinkToolbar, 0, 0);
        try {
            showHamburgerIcon = a.getBoolean(R.styleable.PizzalinkToolbar_showHamburgerMenuIcon, false);
            showBackIcon = a.getBoolean(R.styleable.PizzalinkToolbar_showBackIcon, false);
            title = a.getString(R.styleable.PizzalinkToolbar_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);
        backIcon = (ImageView) rootView.findViewById(R.id.backIcon);
        if(showHamburgerIcon)
            hamburgerIcon.setVisibility(VISIBLE);
        if(showBackIcon)
            backIcon.setVisibility(VISIBLE);
        backIcon.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                BaseActivity.currentActivity.onBackPressed();
            }
        });
        if(title != null) {
            toolbarTitleTextView.setText(title);
            toolbarTitleTextView.setVisibility(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;
    }
}
