package com.sheep.gamegroup.view.fragment;

import android.app.Activity;
import android.os.Bundle;
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.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.sheep.gamegroup.helper.ImageListHelper;
import com.sheep.gamegroup.model.entity.GameEntity;
import com.sheep.gamegroup.model.entity.TaskDescEntity;
import com.sheep.gamegroup.util.CommonUtil;
import com.sheep.gamegroup.util.DataUtil;
import com.sheep.gamegroup.util.ListUtil;
import com.sheep.gamegroup.util.MyListview;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.gamegroup.view.adapter.ArrayAdapter;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.base.BaseFragment;
import com.tencent.smtt.sdk.WebView;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * 玩转游戏 详情
 * Created by ljy on 2018/9/4.
 */

public class FgtPlayGameDetail extends BaseFragment {
    @BindView(R.id.find_game_list)
    RecyclerView find_game_list;
    @BindView(R.id.find_game_info_wv)
    WebView find_game_info_wv;
    @BindView(R.id.item_detail_listview)
    MyListview item_detail_listview;
    @BindView(R.id.progressBarHorizontal)
    ProgressBar progressBarHorizontal;
    @BindView(R.id.last_num_tv)
    TextView last_num_tv;
    @BindView(R.id.progress_title_layout)
    LinearLayout progress_title_layout;
    @BindView(R.id.receive_step_layout)
    LinearLayout receive_step_layout;
    Unbinder unbinder;

    private ArrayList<String> pictureList = ListUtil.emptyList();
    private Activity activity;
    private GameEntity gameEntity;

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

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

    @Override
    public void onViewCreated() {
        activity = getActivity();
        ImageListHelper.initList(activity, find_game_list, pictureList);

        if(gameEntity == null || gameEntity.getApp() == null )
            return;

        //游戏图片banner
        String pictures = gameEntity.getApp().getPictures();
        if (!TextUtils.isEmpty(pictures) && pictures.contains(";")) {
            pictureList.clear();
            ListUtil.addAll(pictureList, pictures.split(";"));
            find_game_list.setVisibility(View.VISIBLE);
            find_game_list.getAdapter().notifyDataSetChanged();
        } else {
            find_game_list.setVisibility(View.GONE);
        }
        ViewUtil.loadDataWithBaseURL(find_game_info_wv, gameEntity.getApp().getIntro());

        //奖励领取步骤
        if(gameEntity.getRelease_task() != null && gameEntity.getRelease_task().getTask().getDescEntityList() != null && gameEntity.getRelease_task().getTask().getDescEntityList().size() > 0){
            receive_step_layout.setVisibility(View.VISIBLE);
            item_detail_listview.setAdapter(new ArrayAdapter<TaskDescEntity>(activity, R.layout.textview_layout, gameEntity.getRelease_task().getTask().getDescEntityList()){
                @Override
                public boolean convert(int position, View convertView, ViewGroup parent, TaskDescEntity item) {
                    TaskDescEntity taskDescEntity = item;
                    if(taskDescEntity == null){
                        return true;
                    }
                    TextView textView = convertView.findViewById(R.id.textview);
                    CommonUtil.getInstance()
                            .bindDataTaskDetailSonStep(taskDescEntity, textView, position);
                    return true;
                }

                @Override
                public boolean areAllItemsEnabled() {
                    return false;
                }
            });
        }else {
            receive_step_layout.setVisibility(View.GONE);
        }
        int progress;
        if(gameEntity.getRelease_task() != null && Integer.valueOf(gameEntity.getRelease_task().getRelease_num()) != 0){
            progress_title_layout.setVisibility(View.VISIBLE);
            progress = Integer.valueOf(gameEntity.getRelease_task().getLast_num())* 100 / Integer.valueOf(gameEntity.getRelease_task().getRelease_num()) ;
            //福利剩余数量
            progressBarHorizontal.incrementProgressBy(progress);
            last_num_tv.setText(gameEntity.getRelease_task().getLastNumText());
        }else {
            progress = 0;
            progress_title_layout.setVisibility(View.GONE);
        }
    }

    @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;
    }
}
