package com.sheep.gamegroup.view.adapter;

import android.app.Activity;
import android.content.Context;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sheep.gamegroup.event.BigEvent;
import com.sheep.gamegroup.event.EventTypes;
import com.sheep.gamegroup.model.entity.DialogConfig;
import com.sheep.gamegroup.model.entity.GameAccountEntity;
import com.sheep.gamegroup.model.entity.RecyleObj;
import com.sheep.gamegroup.model.entity.RecyleType;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.utils.G;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

import rx.functions.Action1;

import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_DEL;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_DEL_COMMIT;
import static com.sheep.gamegroup.util.UMConfigUtils.Event.USER_GAME_ACCOUNT_LOOK;

/**
 * Created by ljy on 2018/7/10.
 */

public class AdpGameAccount extends AdbCommonRecycler<RecyleObj> {

    private Context context;
    private int width;

    public AdpGameAccount(Context context) {
        super(context);
        this.context = context;
    }

    @Override
    public int getItemViewType(int position) {
        RecyleObj item = ListUtil.getItem(listData, position);
        return item != null ? item.getRecyleType().ordinal() : position;
    }

    @Override
    public int getViewIdByType(int type) {
        int resId = R.layout.empty;
        RecyleType recyleType = RecyleType.values()[type];
        switch (recyleType) {
            case GAME_ACCOUNT_PLATFORM:
            case GAME_ACCOUNT_TENCENT:
            case GAME_ACCOUNT_XIAOMI:
                resId = R.layout.try_mkmy_release_task_list;
                break;
        }
        return resId;
    }

    @Override
    public void convert(ViewHolder holder, RecyleObj recyleObj) {
            final List<GameAccountEntity> etyList = (List<GameAccountEntity>) recyleObj.getDataObj();
            if (etyList == null)
                return;
            TextView item_title_tv = holder.itemView.findViewById(R.id.item_title_tv);
            item_title_tv.setVisibility(View.VISIBLE);
            item_title_tv.setTextColor(context.getResources().getColor(R.color.black_444444));
            item_title_tv.setPadding(22, 3, 3, 3);
            switch (recyleObj.getRecyleType()) {
                case GAME_ACCOUNT_PLATFORM:
                    item_title_tv.setText("快发平台账号");
                    break;
                case GAME_ACCOUNT_TENCENT:
                    item_title_tv.setText("腾讯游戏账号");
                    break;
                case GAME_ACCOUNT_XIAOMI:
                    item_title_tv.setText("小米游戏账号");
                    break;
            }
            RecyclerView recyclerView = holder.getView(R.id.release_task_listview, RecyclerView.class);
            recyclerView.setHasFixedSize(true);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));

            recyclerView.setAdapter(new AdbCommonRecycler<GameAccountEntity>(context, etyList) {


                @Override
                public int getViewIdByType(int type) {
                    return R.layout.adp_game_account_item;
                }

                @Override
                public void convert(ViewHolder holder, final GameAccountEntity item) {
                    if (item == null) {
                        return;
                    }
                    final TextView account_tv = holder.itemView.findViewById(R.id.account_tv);
                    TextView state_tv = holder.itemView.findViewById(R.id.state_tv);
                    TextView look_tv = holder.itemView.findViewById(R.id.look_tv);
                    View line_view = holder.itemView.findViewById(R.id.line_view);
                    LinearLayout delete_layout = holder.itemView.findViewById(R.id.delete_layout);


                    account_tv.setText("账号：" + CommonUtil.getInstance().returnStarData(item.getAccount(), 4, 2, 3));

                    account_tv.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) account_tv.getLayoutParams();
                            int widths = account_tv.getMeasuredWidth();
                            if (widths > width) {
                                width = widths;
                            }
                            layoutParams.width = width;
                            layoutParams.height = RelativeLayout.LayoutParams.WRAP_CONTENT;
                            account_tv.setLayoutParams(layoutParams);
                        }
                    }, 10);
                    state_tv.setText(item.getTypeName());
                    look_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            USER_GAME_ACCOUNT_LOOK.onEvent();
                            ViewUtil.newInstance().showGameAccountDetail((Activity) context, item);
                        }
                    });
                    if (item.cantDeleteAccount()) {
                        delete_layout.setVisibility(View.INVISIBLE);
                    } else {
                        delete_layout.setVisibility(View.VISIBLE);
                        delete_layout.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                USER_GAME_ACCOUNT_DEL.onEvent();
                                ViewUtil.showMsgDialog(context, new DialogConfig().setTitle("删除确认").setMsg("确定删除吗？").setBtnRightText("取消")
                                        .setBtnLeftText("确定").setBtnLeftOnClickListener(new View.OnClickListener() {
                                            @Override
                                            public void onClick(View view) {
                                                USER_GAME_ACCOUNT_DEL_COMMIT.onEvent();
                                                CommonUtil.getInstance().deleteGameAccount(context, item.getId(), new Action1<Integer>() {
                                                    @Override
                                                    public void call(Integer integer) {
                                                        if (integer == 1) {
                                                            G.showToast("删除成功!");
                                                            EventBus.getDefault().post(new BigEvent().setEventTypes(EventTypes.DELETE_GAME_ACCOUNT_REFRESH));
                                                        } else {
                                                            G.showToast("删除失败!");
                                                        }
                                                    }
                                                });
                                            }
                                        }));
                            }
                        });
                    }
                }

                @Override
                public void onBindViewHolder(ViewHolder viewHolder, int i) {
                    super.onBindViewHolder(viewHolder, i);

                    viewHolder.updatePosition(i);
                    convert(viewHolder, etyList.get(i));
                    if ((i + 1) == etyList.size()) {
                        viewHolder.itemView.findViewById(R.id.line_view).setVisibility(View.GONE);
                    } else {
                        viewHolder.itemView.findViewById(R.id.line_view).setVisibility(View.VISIBLE);
                    }
                }
            });

    }
}
