package com.secureline.ui;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class SecureLineDashboardRow extends LinearLayout {
    private final TextView labelView;
    private final TextView valueView;
    private final ImageView iconView;
    private final ImageButton copyButton;

    public SecureLineDashboardRow(Context context) { this(context, null); }
    public SecureLineDashboardRow(Context context, AttributeSet attrs) { this(context, attrs, 0); }
    public SecureLineDashboardRow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.CENTER_VERTICAL);
        setMinimumHeight(getResources().getDimensionPixelSize(R.dimen.sl_row_min_height));
        setPadding(dp(16), dp(10), dp(12), dp(10));

        iconView = new ImageView(context);
        LayoutParams ip = new LayoutParams(dp(38), dp(38));
        iconView.setLayoutParams(ip);
        iconView.setPadding(dp(9), dp(9), dp(9), dp(9));
        iconView.setBackgroundResource(R.drawable.sl_icon_container);
        addView(iconView);

        LinearLayout textGroup = new LinearLayout(context);
        textGroup.setOrientation(VERTICAL);
        LayoutParams tg = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f);
        tg.leftMargin = dp(14);
        textGroup.setLayoutParams(tg);
        addView(textGroup);

        labelView = new TextView(context);
        labelView.setTextAppearance(R.style.SecureLineUI_Text_BodySmall);
        textGroup.addView(labelView);

        valueView = new TextView(context);
        valueView.setTextAppearance(R.style.SecureLineUI_Text_Heading);
        LayoutParams vp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        vp.topMargin = dp(3);
        valueView.setLayoutParams(vp);
        textGroup.addView(valueView);

        copyButton = new ImageButton(context);
        copyButton.setImageResource(R.drawable.sl_ic_copy);
        copyButton.setBackgroundColor(android.graphics.Color.TRANSPARENT);
        copyButton.setContentDescription(context.getString(R.string.sl_copy_value));
        copyButton.setPadding(dp(10), dp(10), dp(10), dp(10));
        addView(copyButton, new LayoutParams(dp(44), dp(44)));
        copyButton.setOnClickListener(v -> copyValue());

        boolean copyable = false;
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SecureLineDashboardRow);
            setLabel(a.getText(R.styleable.SecureLineDashboardRow_slRowLabel));
            setValue(a.getText(R.styleable.SecureLineDashboardRow_slRowValue));
            Drawable icon = a.getDrawable(R.styleable.SecureLineDashboardRow_slRowIcon);
            if (icon != null) iconView.setImageDrawable(icon);
            copyable = a.getBoolean(R.styleable.SecureLineDashboardRow_slRowCopyable, false);
            a.recycle();
        }
        if (iconView.getDrawable() == null) iconView.setImageResource(R.drawable.sl_ic_info);
        setCopyable(copyable);
    }

    public void setLabel(CharSequence value) { labelView.setText(value); }
    public void setValue(CharSequence value) { valueView.setText(value); }
    public CharSequence getValue() { return valueView.getText(); }
    public void setValueColor(int color) { valueView.setTextColor(color); }
    public void setCopyable(boolean value) { copyButton.setVisibility(value ? VISIBLE : GONE); }
    public void setIconResource(int resourceId) { iconView.setImageResource(resourceId); }
    private void copyValue() {
        ClipboardManager clipboard = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE);
        if (clipboard != null) clipboard.setPrimaryClip(ClipData.newPlainText(labelView.getText(), valueView.getText()));
    }
    private int dp(int value) { return Math.round(value * getResources().getDisplayMetrics().density); }
}
