package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.sheep.gamegroup.model.entity.Article;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.EveryDayShare;
import com.sheep.gamegroup.model.entity.UserEntity;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.LogUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.util.share.ShareLinkConfig;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;

import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import rx.functions.Action1;

/**
 * Created by realicing on 2018/11/23.
 * realicing@sina.com
 * 小绵羊3.4.5新增--每日分享
 */
public class AdpEveryDayShare extends AdbCommonRecycler<EveryDayShare> {
    public AdpEveryDayShare(Context context, List<EveryDayShare> list) {
        super(context, list);
        list.add(0, new EveryDayShare());
    }

    @Override
    public int getItemViewType(int position) {
        return position == 0 ? 0 : 1;
    }

    @Override
    public int getViewIdByType(int type) {
        if (type == 0) {
            return R.layout.item_text_tips;
        }
        return R.layout.item_every_day_share;
    }

    @Override
    public void convert(ViewHolder holder, final EveryDayShare item) {
        if (item.getId() == 0) {

        } else {
            TextView share_item_title = holder.itemView.findViewById(R.id.share_item_title);
            ImageView share_item_iv = holder.itemView.findViewById(R.id.share_item_iv);
            ViewUtil.setText(share_item_title, item.getTitle());
//        ViewUtil.setImage(share_item_iv, item.getPictures());
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    CommonUtil.getInstance().callActionWithUserInfo(new Action1<UserEntity>() {
                        @Override
                        public void call(UserEntity userEntity) {
                            if (userEntity != null) {
                                ViewUtil.shareLink((Activity) context,
                                        "分享",
                                        userEntity.getShareLink(ShareLinkConfig.OTHER),
                                        item.getTitle(),
                                        item.getContent(),
                                        new ViewUtil.ShareCallback() {
                                            @Override
                                            public void callback() {
                                                SheepApp.getInstance().getNetComponent().getApiService()
                                                        .sendShareResult(item.getId())
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(Schedulers.io())
                                                        .subscribe(new SheepSubscriber<BaseMessage>(SheepApp.getInstance()) {
                                                            @Override
                                                            public void onNext(BaseMessage baseMessage) {
                                                            }

                                                            @Override
                                                            public void onError(BaseMessage baseMessage) {
                                                                LogUtil.logE(baseMessage.getCode() + " : " + baseMessage.getErrorMsg());
                                                            }
                                                        });
                                            }
                                        });
                            }
                        }
                    });
                }
            });
        }
    }
}
