package com.kdanmobile.kdan_others_library_for_android.card;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.kdanmobile.kdan_others_library_for_android.R;

public class CreativeStorePromoteCard extends LinearLayout {

    private OnClickListener onClickViewNowListener;

    private OnClickListener onClickImageListener;

    private OnClickListener onClickFreeTrialListener;

    private Button viewNow;

    private Button freeTrial;

    public CreativeStorePromoteCard(Context context) {
        super(context);
        initView();
    }

    public CreativeStorePromoteCard(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    public CreativeStorePromoteCard(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        initView();
    }

    public void setButtonColor(int color) {
        viewNow.setTextColor(color);
        freeTrial.setTextColor(color);
    }

    private void initView() {
        View view = inflate(getContext(), R.layout.widget_creative_store_promote_card, this);
        viewNow = (Button) view.findViewById(R.id.button_promote_card_creative_store_view_now);
        freeTrial = (Button) view.findViewById(R.id.button_promote_card_creative_store_free_trial);
        ImageView imageView = (ImageView) view.findViewById(R.id.image_view_promote_card_creative_store_view);

        viewNow.setOnClickListener(view1 -> {
            if (onClickViewNowListener != null) {
                onClickViewNowListener.onClick(view1);
            } else {
                intentToCreativeStore();
            }
        });

        imageView.setOnClickListener(view12 -> {
            if (onClickImageListener != null) {
                onClickImageListener.onClick(view12);
            } else {
                intentToCreativeStore();
            }
        });

        freeTrial.setOnClickListener(view13 -> {
            if (onClickFreeTrialListener != null) {
                onClickFreeTrialListener.onClick(view13);
            }
        });

    }

    public void setOnClickViewNowListener(OnClickListener onClickViewNowListener) {
        this.onClickViewNowListener = onClickViewNowListener;
    }

    public void setOnClickImageListener(OnClickListener onClickImageListener) {
        this.onClickImageListener = onClickImageListener;
    }

    public void setOnClickFreeTrialListener(OnClickListener onClickFreeTrialListener) {
        this.onClickFreeTrialListener = onClickFreeTrialListener;
    }

    private void intentToCreativeStore() {
        Uri uri = Uri.parse("https://creativestore.kdanmobile.com/subscription/creativity365?utm_source=Website&utm_campaign=KdanWeb_NL_C365&utm_medium=NL");
        Intent intent = new Intent(Intent.ACTION_VIEW, uri);
        getContext().startActivity(intent);
    }
}
