package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.GiftBagApp;
import com.sheep.gamegroup.model.entity.Release_task;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ViewHolder;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.AdbCommonRecycler;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;
import rx.functions.Action1;

/**
 * Created by ljy on 2018/9/4.
 */

public class FgtPlaGameGift extends BaseFragment {
    @BindView(R.id.recyclerView)
    RecyclerView recyclerviewList;
    Unbinder unbinder;

    private Activity activity;
    private GameEntity gameEntity;
    private List<GiftBagApp> has_gift_bag = new ArrayList<>();
    private List<Object> objectList = new ArrayList<>();

    public static FgtPlaGameGift newInstance(GameEntity gameEntity){
        FgtPlaGameGift fgt = new FgtPlaGameGift();
        Bundle bundle = new Bundle();
        DataUtil.putObject(bundle, gameEntity);
        fgt.setArguments(bundle);
        return fgt;
    }

    @Override
    public int getLayoutId() {
        return R.layout.common_rv;
    }

    @Override
    public void onViewCreated() {
        if(gameEntity == null)
            return;
        has_gift_bag.clear();
        has_gift_bag.addAll(gameEntity.getHas_gift_bag());
        objectList.clear();
        if(!TextUtils.isEmpty(gameEntity.getBonusText()) && gameEntity.getRelease_task() != null)
            objectList.add(gameEntity.getRelease_task());
        objectList.addAll(gameEntity.getHas_gift_bag());

        activity = getActivity();
        recyclerviewList.setLayoutManager(new LinearLayoutManager(SheepApp.getInstance()));
        recyclerviewList.setAdapter(new AdbCommonRecycler<Object>(SheepApp.getInstance(), objectList) {
            @Override
            public int getViewIdByType(int type) {
                return R.layout.fgt_playgame_gift_item;
            }

            @Override
            public void convert(ViewHolder holder, final Object giftBagApp) {
                if (giftBagApp instanceof GiftBagApp){
                    bindGfitData(holder, (GiftBagApp) giftBagApp);
                }else if(giftBagApp instanceof Release_task){
                    bindReleaseData(holder, (Release_task) giftBagApp);
                }
            }

            private void bindGfitData(ViewHolder holder, final GiftBagApp giftBagApp){

                View view = holder.itemView;
                ViewUtil.setText((TextView) holder.getView(R.id.name_tv), giftBagApp.getGift_bag().getGiftName());
                ViewUtil.setText((TextView) holder.getView(R.id.num_tv), giftBagApp.getGift_bag().getLastNumText());

                final TextView btn_tv = holder.getView(R.id.btn_tv);

                if (!giftBagApp.isReceived()){
                    btn_tv.setText("领取");
                    btn_tv.setEnabled(true);
                    btn_tv.setTextColor(activity.getResources().getColor(R.color.txt_bule));
                    btn_tv.setBackgroundResource(R.drawable.shape_blue_stroke_rectangle);
                    btn_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(CommonUtil.getInstance().isInstallApp(gameEntity.getApp().getPackage_name(), gameEntity.getApp().getName())){
                                CommonUtil.getInstance().receiveGiftById(activity, giftBagApp, new Action1<Integer>() {
                                    @Override
                                    public void call(Integer integer) {
                                        giftBagApp.setReceived(true);
                                        btn_tv.setText("已领取");
                                        btn_tv.setEnabled(false);
                                        notifyDataSetChanged();
                                    }
                                },gameEntity.getApp().getPackage_name());
                            }

                        }
                    });
                }else {
                    btn_tv.setText("已领取");
                    btn_tv.setEnabled(false);
                    btn_tv.setTextColor(activity.getResources().getColor(R.color.time_created));
                    btn_tv.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                }
                holder.getView(R.id.line_tv).setVisibility(holder.getAdapterPosition() + 1 == getItemCount() ? View.GONE : View.VISIBLE);
            }

            private void bindReleaseData(ViewHolder holder, final Release_task giftBagApp){

                View view = holder.itemView;
                ViewUtil.setText((TextView) holder.getView(R.id.name_tv), giftBagApp.getName());
                ViewUtil.setText((TextView) holder.getView(R.id.num_tv), giftBagApp.getLastNumText());

                final TextView btn_tv = holder.getView(R.id.btn_tv);

                if (!gameEntity.getReceived_voucher()){
                    btn_tv.setText("领取");
                    btn_tv.setEnabled(true);

                    btn_tv.setTextColor(activity.getResources().getColor(R.color.txt_bule));
                    btn_tv.setBackgroundResource(R.drawable.shape_blue_stroke_rectangle);
                    btn_tv.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if(CommonUtil.getInstance().isInstallApp(giftBagApp.getTask().getPackage_names(), giftBagApp.getName())){
                                CommonUtil.getInstance().receiveAward(activity, gameEntity, new Action1<Integer>() {
                                    @Override
                                    public void call(Integer integer) {
                                        gameEntity.setReceived_voucher(true);
                                        btn_tv.setText("已领取");
                                        btn_tv.setEnabled(false);
                                        notifyDataSetChanged();
                                    }
                                });

                            }
                        }
                    });
                }else {
                    btn_tv.setText("已领取");
                    btn_tv.setEnabled(false);
                    btn_tv.setTextColor(activity.getResources().getColor(R.color.time_created));
                    btn_tv.setBackgroundResource(R.drawable.shape_gray_stroke_rectangle_10_radius);
                }
                holder.getView(R.id.line_tv).setVisibility(holder.getAdapterPosition() + 1 == getItemCount() ? View.GONE : View.VISIBLE);
            }

        });

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO: inflate a fragment view
        View rootView = super.onCreateView(inflater, container, savedInstanceState);
        gameEntity = DataUtil.getObject(getArguments(), GameEntity.class);
        return rootView;
    }
}
