package com.secureline.ui;

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

/** Premium Lemenzo Protect card. Internal class name is intentionally unchanged. */
public class SecureLineProtectionRow extends LinearLayout {
    private final TextView titleView;
    private final TextView subtitleView;
    private final TextView readMoreView;
    private final ImageView iconView;
    private final SecureLineStatusBadge badgeView;
    private final LinearLayout inlineContainer;

    public SecureLineProtectionRow(Context context) { this(context, null); }
    public SecureLineProtectionRow(Context context, AttributeSet attrs) { this(context, attrs, 0); }
    public SecureLineProtectionRow(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(HORIZONTAL);
        setGravity(Gravity.TOP);
        setMinimumHeight(dp(132));
        setPadding(dp(16), dp(14), dp(14), dp(12));
        setBackgroundResource(R.drawable.sl_protection_card);
        setClipToPadding(false);
        setClickable(true);
        setFocusable(true);
        setElevation(dp(2));

        iconView = new ImageView(context);
        iconView.setBackgroundResource(R.drawable.sl_icon_container_premium);
        iconView.setPadding(dp(8), dp(8), dp(8), dp(8));
        iconView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
        iconView.setColorFilter(android.graphics.Color.WHITE, android.graphics.PorterDuff.Mode.SRC_IN);
        LayoutParams iconParams = new LayoutParams(dp(68), dp(68));
        iconParams.topMargin = dp(1);
        addView(iconView, iconParams);

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

        LinearLayout titleRow = new LinearLayout(context);
        titleRow.setOrientation(HORIZONTAL);
        titleRow.setGravity(Gravity.CENTER_VERTICAL);
        content.addView(titleRow, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));

        titleView = new TextView(context);
        titleView.setTextAppearance(R.style.SecureLineUI_Text_Title);
        titleView.setTextSize(17);
        titleView.setMaxLines(2);
        titleView.setEllipsize(android.text.TextUtils.TruncateAt.END);
        titleRow.addView(titleView, new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1f));

        badgeView = new SecureLineStatusBadge(context);
        badgeView.setClickable(true);
        badgeView.setFocusable(true);
        LayoutParams badgeParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        badgeParams.leftMargin = dp(8);
        titleRow.addView(badgeView, badgeParams);

        subtitleView = new TextView(context);
        subtitleView.setTextAppearance(R.style.SecureLineUI_Text_BodySmall);
        subtitleView.setTextSize(13);
        subtitleView.setMaxLines(3);
        subtitleView.setEllipsize(android.text.TextUtils.TruncateAt.END);
        LayoutParams subtitleParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        subtitleParams.topMargin = dp(5);
        content.addView(subtitleView, subtitleParams);

        inlineContainer = new LinearLayout(context);
        inlineContainer.setOrientation(VERTICAL);
        inlineContainer.setVisibility(GONE);
        LayoutParams inlineParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        inlineParams.topMargin = dp(10);
        content.addView(inlineContainer, inlineParams);

        readMoreView = new TextView(context);
        readMoreView.setText(R.string.sl_read_more);
        readMoreView.setTextColor(getResources().getColor(R.color.sl_color_blue_soft, context.getTheme()));
        readMoreView.setTextSize(13);
        readMoreView.setTypeface(android.graphics.Typeface.create("sans-serif-medium", 0));
        readMoreView.setGravity(Gravity.CENTER_VERTICAL);
        readMoreView.setBackgroundResource(R.drawable.sl_read_more_row);
        readMoreView.setCompoundDrawablesRelativeWithIntrinsicBounds(0, 0, R.drawable.sl_ic_chevron_right_blue, 0);
        readMoreView.setCompoundDrawablePadding(dp(6));
        readMoreView.setPadding(dp(11), 0, dp(10), 0);
        readMoreView.setMinHeight(dp(36));
        readMoreView.setClickable(true);
        readMoreView.setFocusable(true);
        LayoutParams readMoreParams = new LayoutParams(LayoutParams.MATCH_PARENT, dp(36));
        readMoreParams.topMargin = dp(9);
        content.addView(readMoreView, readMoreParams);

        boolean enabled = false;
        if (attrs != null) {
            TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SecureLineProtectionRow);
            setTitle(a.getText(R.styleable.SecureLineProtectionRow_slProtectionTitle));
            setSubtitle(a.getText(R.styleable.SecureLineProtectionRow_slProtectionSubtitle));
            Drawable icon = a.getDrawable(R.styleable.SecureLineProtectionRow_slProtectionIcon);
            if (icon != null) iconView.setImageDrawable(icon);
            enabled = a.getBoolean(R.styleable.SecureLineProtectionRow_slProtectionEnabled, false);
            a.recycle();
        }
        if (iconView.getDrawable() == null) iconView.setImageResource(R.drawable.sl_ic_shield);
        setProtectionEnabled(enabled);
    }

    public void setTitle(CharSequence value) { titleView.setText(value); }
    public void setSubtitle(CharSequence value) {
        subtitleView.setText(value);
        subtitleView.setVisibility(value == null || value.length() == 0 ? GONE : VISIBLE);
    }
    public void setProtectionEnabled(boolean value) {
        badgeView.setText(value ? R.string.sl_on : R.string.sl_off);
        badgeView.setBadgeType(value ? SecureLineStatusBadge.TYPE_ACTIVE : SecureLineStatusBadge.TYPE_NEUTRAL);
    }
    public void setStatus(CharSequence text, int badgeType) {
        badgeView.setVisibility(VISIBLE);
        badgeView.setText(text);
        badgeView.setBadgeType(badgeType);
    }
    public void setStatusClickable(boolean clickable) {
        badgeView.setClickable(clickable);
        badgeView.setFocusable(clickable);
        badgeView.setAlpha(clickable ? 1f : .88f);
    }
    public void setOnStatusClickListener(OnClickListener listener) { badgeView.setOnClickListener(listener); }
    public void setOnReadMoreClickListener(OnClickListener listener) { readMoreView.setOnClickListener(listener); }
    public void setIconResource(int resourceId) {
        iconView.setImageResource(resourceId);
        iconView.setColorFilter(android.graphics.Color.WHITE, android.graphics.PorterDuff.Mode.SRC_IN);
    }
    public void useSwitchControl() { badgeView.setVisibility(VISIBLE); }
    public LinearLayout getInlineContainer() { inlineContainer.setVisibility(VISIBLE); return inlineContainer; }
    public LinearLayout getInlineHorizontalContainer() {
        inlineContainer.setVisibility(VISIBLE);
        inlineContainer.setOrientation(HORIZONTAL);
        inlineContainer.setGravity(Gravity.CENTER_VERTICAL);
        return inlineContainer;
    }
    public void setIconVisualSize(int frameDp, int paddingDp) {
        LayoutParams params = (LayoutParams) iconView.getLayoutParams();
        params.width = dp(frameDp);
        params.height = dp(frameDp);
        iconView.setLayoutParams(params);
        iconView.setPadding(dp(paddingDp), dp(paddingDp), dp(paddingDp), dp(paddingDp));
    }
    private int dp(int value) { return Math.round(value * getResources().getDisplayMetrics().density); }
}
