package kr.co.hanwha.hifive.admin.admin.adapter.viewholder;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.SpannableString;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import kr.co.hanwha.hifive.R;
import kr.co.hanwha.hifive.admin.admin.adapter.AdminVersionListAdapter;
import kr.co.hanwha.hifive.admin.network.model.AdminVersionCvData;
import kr.co.hanwha.hifive.base.BaseViewHolder;
import kr.co.hanwha.hifive.util.HFDateUtil;
import kr.co.hanwha.hifive.util.HFSpanUtil;
import kr.co.hanwha.hifive.util.HFStringUtil;

public class AdminVersionListViewHolder extends BaseViewHolder {
    private LinearLayout mLayoutContainer;

    private TextView mTextViewPlatform;
    private TextView mTextViewStringBundle;
    private TextView mTextViewVersion;
    private ImageView mImageViewForced;
    private TextView mTextViewUpdate;
    private TextView mTextViewNotice;
    private TextView mTextViewUrl;

    private TextView mTextViewDelete;
    private TextView mTextViewEdit;

    private AdminVersionListAdapter.OnAdminVersionListListener mOnAdminVersionListListener;

    public AdminVersionListViewHolder(@NonNull ViewGroup viewGroup,
                                      @Nullable AdminVersionListAdapter.OnAdminVersionListListener onAdminVersionListListener) {

        super(viewGroup, R.layout.item_admin_version_list);

        mOnAdminVersionListListener = onAdminVersionListListener;

        mLayoutContainer = (LinearLayout) itemView.findViewById(R.id.container_linearLayout);

        mTextViewPlatform = (TextView) itemView.findViewById(R.id.platform_textView);
        mTextViewStringBundle = (TextView) itemView.findViewById(R.id.stringBundle_textView);
        mTextViewVersion = (TextView) itemView.findViewById(R.id.version_textView);
        mImageViewForced = (ImageView) itemView.findViewById(R.id.forced_imageView);
        mTextViewUpdate = (TextView) itemView.findViewById(R.id.update_textView);
        mTextViewNotice = (TextView) itemView.findViewById(R.id.notice_textView);
        mTextViewUrl = (TextView) itemView.findViewById(R.id.url_textView);

        mTextViewDelete = (TextView) itemView.findViewById(R.id.delete_textView);
        mTextViewEdit = (TextView) itemView.findViewById(R.id.edit_textView);
    }

    @Override
    public void onBind(Object object, final int index) {
        super.onBind(object, index);

        if (object != null && object instanceof AdminVersionCvData) {
            final AdminVersionCvData data = (AdminVersionCvData) object;
            try {
                mTextViewPlatform.setText(data.getPlatform_type_desc() + "(" + data.getPlatform_type() + ")");
                mTextViewStringBundle.setText(data.getString_bundle());
                mTextViewVersion.setText(data.getVersion());
                mImageViewForced.setSelected(data.isIs_forced());
                mTextViewUpdate.setText(HFDateUtil.getChangeDateFormat(data.getReleased_at(), HFDateUtil.FORMAT_COMMON_SERVER, HFDateUtil.FORMAT_COMMON_SERVER));
                mTextViewNotice.setText(data.getUpdate_notice());
                setUrlText(mTextViewUrl, data.getUpdate_url(), mOnAdminVersionListListener);
            } catch (Exception e) {
                e.printStackTrace();
            }

            if (mOnAdminVersionListListener != null) {
                mLayoutContainer.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
//                        mOnAdminTaskListListener.onTaskClicked(data);
                    }
                });

                mTextViewEdit.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnAdminVersionListListener.onEditClicked(data);
                    }
                });

                mTextViewDelete.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        mOnAdminVersionListListener.onDeleteClicked(data.getId());
                    }
                });
            }
        }
    }

    public static void setUrlText(@NonNull TextView textView,
                                  @Nullable String message,
                                  @Nullable final AdminVersionListAdapter.OnAdminVersionListListener onAdminVersionListListener) {
        if (TextUtils.isEmpty(message) == false) {
            String[] links = null;
            try {
                links = HFStringUtil.extractLinks(message);
            } catch (Exception e) {
                e.printStackTrace();
            }
            SpannableString styledString = new SpannableString(message);

            if (links != null) {
                for (int i = 0; i < links.length; i++) {
                    int startPosition = message.indexOf(links[i]);
                    int endPosition = startPosition + links[i].length();

                    final String url = links[i];

                    ClickableSpan clickableURLSpan = new ClickableSpan() {
                        @Override
                        public void onClick(View widget) {
                            if (onAdminVersionListListener != null)
                                onAdminVersionListListener.onUrlLinkClicked(url);
                        }
                    };
                    styledString.setSpan(clickableURLSpan, startPosition, endPosition, 0);
                }
            }

            textView.setText(styledString);
            textView.setMovementMethod(LinkMovementMethod.getInstance());
        } else {
            textView.setText("");
        }
    }
}
/*
{
    "id": 3,

    "platform_type_desc": "iPhone-dev",
    "string_bundle": "Unbundled",

    "version": "0.1.0",

    "is_forced": true,

    "update_notice": "새로운 업데이트 버전이 있습니다.",

    "platform_type": 2,

    "created_at": "2017-11-01 07:30:05.0",
    "updated_at": "2018-03-30 03:46:22.0",
    "released_at": "2017-11-01 07:30:05.0",
    "deleted_at": null
}
*/