package com.danielstone.materialaboutlibrary.items;


import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import androidx.annotation.DrawableRes;
import androidx.annotation.StringRes;

import android.util.Log;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.danielstone.materialaboutlibrary.R;
import com.danielstone.materialaboutlibrary.holders.MaterialAboutItemViewHolder;
import com.danielstone.materialaboutlibrary.util.ViewTypeManager;

import static android.view.View.GONE;

public class MaterialAboutProfileItem extends MaterialAboutItem {

    private CharSequence text = null;
    private int textRes = 0;
    private CharSequence desc = null;
    private int descRes = 0;
    private Drawable icon = null;
    private int iconRes = 0;
    private MaterialAboutItemOnClickAction onClickAction = null;
    private MaterialAboutItemOnClickAction onLongClickAction = null;

    private MaterialAboutProfileItem(MaterialAboutProfileItem.Builder builder) {
        super();
        this.text = builder.text;
        this.textRes = builder.textRes;

        this.desc = builder.desc;
        this.descRes = builder.descRes;

        this.icon = builder.icon;
        this.iconRes = builder.iconRes;

        this.onClickAction = builder.onClickAction;
        this.onLongClickAction = builder.onLongClickAction;
    }

    public MaterialAboutProfileItem(CharSequence text, CharSequence desc, Drawable icon) {
        this.text = text;
        this.desc = desc;
        this.icon = icon;
    }

    public MaterialAboutProfileItem(int textRes, int descRes, int iconRes) {
        this.textRes = textRes;
        this.descRes = descRes;
        this.iconRes = iconRes;
    }

    public static MaterialAboutItemViewHolder getViewHolder(View view) {
        return new MaterialAboutProfileItem.MaterialAboutProfileItemViewHolder(view);
    }

    public static void setupItem(MaterialAboutProfileItemViewHolder holder, MaterialAboutProfileItem item, Context context) {

        CharSequence text = item.getText();
        int textRes = item.getTextRes();

        holder.text.setVisibility(View.VISIBLE);
        if (text != null) {
            holder.text.setText(text);
        } else if (textRes != 0) {
            holder.text.setText(textRes);
        } else {
            holder.text.setVisibility(GONE);
        }

        CharSequence desc = item.getDesc();
        int descRes = item.getDescRes();

        holder.desc.setVisibility(View.VISIBLE);
        if (desc != null) {
            holder.desc.setText(desc);
        } else if (descRes != 0) {
            holder.desc.setText(descRes);
        } else {
            holder.desc.setVisibility(GONE);
        }

        Drawable drawable = item.getIcon();
        int drawableRes = item.getIconRes();
        if (drawable != null) {
            holder.icon.setImageDrawable(drawable);
        } else if (drawableRes != 0) {
            holder.icon.setImageResource(drawableRes);
        }

        int pL = 0, pT = 0, pR = 0, pB = 0;
        if (Build.VERSION.SDK_INT < 21) {
            pL = holder.view.getPaddingLeft();
            pT = holder.view.getPaddingTop();
            pR = holder.view.getPaddingRight();
            pB = holder.view.getPaddingBottom();
        }

        if (item.getOnClickAction() != null || item.getOnLongClickAction() != null) {
            TypedValue outValue = new TypedValue();
            context.getTheme().resolveAttribute(R.attr.selectableItemBackground, outValue, true);
            holder.view.setBackgroundResource(outValue.resourceId);
        } else {
            holder.view.setBackgroundResource(0);
        }
        holder.setOnClickAction(item.getOnClickAction());
        holder.setOnLongClickAction(item.getOnLongClickAction());

        if (Build.VERSION.SDK_INT < 21) {
            holder.view.setPadding(pL, pT, pR, pB);
        }
    }

    @Override
    public int getType() {
        return ViewTypeManager.ItemType.PROFILE_ITEM;
    }

    @Override
    public String getDetailString() {
        return "MaterialAboutProfileItem{" +
                "text=" + text +
                ", textRes=" + textRes +
                ", desc=" + desc +
                ", descRes=" + descRes +
                ", icon=" + icon +
                ", iconRes=" + iconRes +
                ", onClickAction=" + onClickAction +
                ", onLongClickAction=" + onLongClickAction +
                '}';
    }

    public MaterialAboutProfileItem(MaterialAboutProfileItem item) {
        this.id = item.getId();
        this.text = item.getText();
        this.textRes = item.getTextRes();
        this.desc = item.getDesc();
        this.descRes = item.getDescRes();
        this.icon = item.getIcon();
        this.iconRes = item.getIconRes();
        this.onClickAction = item.getOnClickAction();
        this.onLongClickAction = item.getOnLongClickAction();
    }

    @Override
    public MaterialAboutProfileItem clone() {
        return new MaterialAboutProfileItem(this);
    }

    public CharSequence getText() {
        return text;
    }

    public MaterialAboutProfileItem setText(CharSequence text) {
        this.textRes = 0;
        this.text = text;
        return this;
    }

    public int getTextRes() {
        return textRes;
    }

    public MaterialAboutProfileItem setTextRes(int textRes) {
        this.text = null;
        this.textRes = textRes;
        return this;
    }

    public CharSequence getDesc() {
        return desc;
    }

    public MaterialAboutProfileItem setDesc(CharSequence desc) {
        this.descRes = 0;
        this.desc = desc;
        return this;
    }

    public int getDescRes() {
        return descRes;
    }

    public MaterialAboutProfileItem setDescRes(int descRes) {
        this.desc = null;
        this.descRes = textRes;
        return this;
    }

    public Drawable getIcon() {
        return icon;
    }

    public MaterialAboutProfileItem setIcon(Drawable icon) {
        this.iconRes = 0;
        this.icon = icon;
        return this;
    }

    public int getIconRes() {
        return iconRes;
    }

    public MaterialAboutProfileItem setIconRes(int iconRes) {
        this.icon = null;
        this.iconRes = iconRes;
        return this;
    }

    public MaterialAboutItemOnClickAction getOnClickAction() {
        return onClickAction;
    }

    public MaterialAboutProfileItem setOnClickAction(MaterialAboutItemOnClickAction onClickAction) {
        this.onClickAction = onClickAction;
        return this;
    }

    public MaterialAboutItemOnClickAction getOnLongClickAction() {
        return onLongClickAction;
    }

    public MaterialAboutProfileItem setOnLongClickAction(MaterialAboutItemOnClickAction onLongClickAction) {
        this.onLongClickAction = onLongClickAction;
        return this;
    }
    public static class MaterialAboutProfileItemViewHolder extends MaterialAboutItemViewHolder implements View.OnClickListener, View.OnLongClickListener {
        public final View view;
        public final ImageView icon;
        public final TextView text;
        public final TextView desc;
        private MaterialAboutItemOnClickAction onClickAction;
        private MaterialAboutItemOnClickAction onLongClickAction;

        MaterialAboutProfileItemViewHolder(View view) {
            super(view);
            this.view = view;
            icon = (ImageView) view.findViewById(R.id.mal_item_image);
            text = (TextView) view.findViewById(R.id.mal_item_text);
            desc = (TextView) view.findViewById(R.id.mal_item_desc);
        }

        public void setOnClickAction(MaterialAboutItemOnClickAction onClickAction) {
            this.onClickAction = onClickAction;
            if (onClickAction != null) {
                view.setOnClickListener(this);
            } else {
                view.setClickable(false);
            }
        }

        public void setOnLongClickAction(MaterialAboutItemOnClickAction onLongClickAction) {
            this.onLongClickAction = onLongClickAction;
            if (onLongClickAction != null) {
                view.setOnLongClickListener(this);
            } else {
                view.setLongClickable(false);
            }
        }

        @Override
        public void onClick(View v) {
            if (onClickAction != null) {
                onClickAction.onClick();
            }
        }

        @Override
        public boolean onLongClick(View v) {
            if (onLongClickAction != null) {
                onLongClickAction.onClick();
                return true;
            }
            return false;
        }
    }

    public static class Builder {

        MaterialAboutItemOnClickAction onClickAction = null;
        MaterialAboutItemOnClickAction onLongClickAction = null;
        private CharSequence text = null;
        @StringRes
        private int textRes = 0;
        private CharSequence desc = null;
        @StringRes
        private int descRes = 0;
        private Drawable icon = null;
        @DrawableRes
        private int iconRes = 0;

        public MaterialAboutProfileItem.Builder text(CharSequence text) {
            this.text = text;
            this.textRes = 0;
            return this;
        }


        public MaterialAboutProfileItem.Builder text(@StringRes int text) {
            this.textRes = text;
            this.text = null;
            return this;
        }

        public MaterialAboutProfileItem.Builder desc(CharSequence desc) {
            this.desc = desc;
            this.descRes = 0;
            return this;
        }


        public MaterialAboutProfileItem.Builder desc(@StringRes int desc) {
            this.descRes = desc;
            this.desc = null;
            return this;
        }

        public MaterialAboutProfileItem.Builder icon(Drawable icon) {
            this.icon = icon;
            this.iconRes = 0;
            return this;
        }


        public MaterialAboutProfileItem.Builder icon(@DrawableRes int iconRes) {
            this.icon = null;
            this.iconRes = iconRes;
            return this;
        }

        public MaterialAboutProfileItem.Builder setOnClickAction(MaterialAboutItemOnClickAction onClickAction) {
            this.onClickAction = onClickAction;
            return this;
        }

        public MaterialAboutProfileItem.Builder setOnLongClickAction(MaterialAboutItemOnClickAction onLongClickAction) {
            this.onLongClickAction = onLongClickAction;
            return this;
        }

        public MaterialAboutProfileItem build() {
            return new MaterialAboutProfileItem(this);
        }
    }
}
