package com.kfzs.duanduan.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volleyplus.Response;
import com.bumptech.glide.Glide;
import com.kfzs.duanduan.view.KFProgressButton;
import com.kfzs.appstore.utils.adapter.recyclerview.RecyclerViewAdapter;
import com.kfzs.appstore.utils.adapter.recyclerview.ViewHolder;
import com.kfzs.appstore.utils.restful.KFZSNetwork;
import com.sheep.jiuyan.samllsheep.R;
import com.kfzs.duanduan.bean.DownloadStatus;
import com.kfzs.duanduan.utils.InstallButtonUtils;
import com.kfzs.duanduan.bean.KFIntentKeys;
import com.kfzs.duanduan.bll.UrlBll;
import com.kfzs.duanduan.event.EventDownloadHandler;
import com.kfzs.duanduan.proto.ApiResponseOuterClass;
import com.kfzs.duanduan.proto.GameOuterClass;
import com.kfzs.duanduan.utils.dlg.DownBtnUtils;
import com.kfzs.duanduan.utils.dlg.HelperUtils;
import com.kfzs.duanduan.utils.dlg.SkipUtils;
import com.kfzs.duanduan.utils.net.APIRequest;
import com.kfzs.duanduan.utils.net.APIRequestInstance;
import com.kfzs.duanduan.ActPicturesEnlarge;
import com.kfzs.duanduan.BaseCompatFragment;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ Created by Dlg
 * @ <p>TiTle:  FgtGameDetail</p>
 * @ <p>Description: 商品详情页的详情页面</p>
 * @ date:  2017/6/19 13:36
 * @ QQ:    315096953
 */

public class FgtGameDetail extends BaseCompatFragment {


    private RecyclerView mRvImags;
    private TextView mGameDesc;
    private ImageView mGsIcon1;
    private ImageView mGsIcon2;
    private ImageView mGsIcon3;
    private ImageView mGsIcon4;
    private TextView mGsName1;
    private TextView mGsName2;
    private TextView mGsName3;
    private TextView mGsName4;
    private KFProgressButton mGsBtn1;
    private KFProgressButton mGsBtn2;
    private KFProgressButton mGsBtn3;
    private KFProgressButton mGsBtn4;
    private RecyclerViewAdapter mRecyclerViewAdapter;

    private Map<String, InstallButtonUtils> mInstallButtonMgr = new HashMap<>();
    private EventDownloadHandler.OnDownCallback mOnDownCallback;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.fgt_game_detail);
        initViewById();
        getGsGame();
    }


    @Subscribe
    public void onEventMainThread(DownloadStatus info) {
        if (mOnDownCallback == null) {
            mOnDownCallback = DownBtnUtils.installBtnStatus(mInstallButtonMgr);
        }
        EventDownloadHandler.newInstance(getContext(), mOnDownCallback)
                .setmTag(TAG).handlDownloadResult(info);
    }

    /**
     * guess the fuck games；
     */
    private void getGsGame() {
        String gameId = getActivity().getIntent().getStringExtra(KFIntentKeys.EXTRA_GAME_ID);
        //        http://10.8.210.223:8081/test/game/guess?game_id=125
        String url = UrlBll.GameApi.GAME_GUESS + "game_id=" + gameId;

        APIRequest gsRequest = APIRequestInstance.getReq(TAG, url, null, new Response.Listener<ApiResponseOuterClass.ApiResponse>() {
            @Override
            public void onResponse(ApiResponseOuterClass.ApiResponse apiResponse) {
                List<GameOuterClass.Game> gsGamesList = apiResponse.getGamesList();
                //try for out of index
                try {
                    Glide.with(FgtGameDetail.this).load(gsGamesList.get(0).getIconImage()).into(mGsIcon1);
                    Glide.with(FgtGameDetail.this).load(gsGamesList.get(1).getIconImage()).into(mGsIcon2);
                    Glide.with(FgtGameDetail.this).load(gsGamesList.get(2).getIconImage()).into(mGsIcon3);
                    Glide.with(FgtGameDetail.this).load(gsGamesList.get(3).getIconImage()).into(mGsIcon4);
                    mGsName1.setText(gsGamesList.get(0).getGameName());
                    mGsName2.setText(gsGamesList.get(1).getGameName());
                    mGsName3.setText(gsGamesList.get(2).getGameName());
                    mGsName4.setText(gsGamesList.get(3).getGameName());
                    mGsIcon1.setOnClickListener(guessYouLikeLisener);
                    mGsIcon1.setTag(R.id.view_tag_game_icon, gsGamesList.get(0).getId());
                    mGsIcon2.setOnClickListener(guessYouLikeLisener);
                    mGsIcon2.setTag(R.id.view_tag_game_icon, gsGamesList.get(1).getId());
                    mGsIcon3.setOnClickListener(guessYouLikeLisener);
                    mGsIcon3.setTag(R.id.view_tag_game_icon, gsGamesList.get(2).getId());
                    mGsIcon4.setOnClickListener(guessYouLikeLisener);
                    mGsIcon4.setTag(R.id.view_tag_game_icon, gsGamesList.get(3).getId());


                    DownBtnUtils.addDownloadBtnListener(mGsBtn1, getContext(), mInstallButtonMgr, gsGamesList.get(0), "gsGame_01");
                    DownBtnUtils.addDownloadBtnListener(mGsBtn2, getContext(), mInstallButtonMgr, gsGamesList.get(1), "gsGame_02");
                    DownBtnUtils.addDownloadBtnListener(mGsBtn3, getContext(), mInstallButtonMgr, gsGamesList.get(2), "gsGame_03");
                    DownBtnUtils.addDownloadBtnListener(mGsBtn4, getContext(), mInstallButtonMgr, gsGamesList.get(3), "gsGame_04");

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }, HelperUtils.makeErr(getString(R.string.toast_init_data_fail)));

        KFZSNetwork.addRequest(gsRequest);
    }


    /**
     * 点猜你喜欢中的icon，跳游戏详情
     */
    private View.OnClickListener guessYouLikeLisener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            SkipUtils.getInstance().goGameDetails(getActivity(), (int) v.getTag(R.id.view_tag_game_icon));
        }
    };

    private void initViewById() {
        mRvImags = mContentView.findViewById(R.id.recy_fgt_game_detail);
        mGameDesc = mContentView.findViewById(R.id.tv_game_desc);
        mGsIcon1 = mContentView.findViewById(R.id.img_fgt_fgt_detail_1);
        mGsIcon2 = mContentView.findViewById(R.id.img_fgt_fgt_detail_2);
        mGsIcon3 = mContentView.findViewById(R.id.img_fgt_fgt_detail_3);
        mGsIcon4 = mContentView.findViewById(R.id.img_fgt_fgt_detail_4);
        mGsName1 = mContentView.findViewById(R.id.txt_fgt_game_detail_1);
        mGsName2 = mContentView.findViewById(R.id.txt_fgt_fgt_detail_2);
        mGsName3 = mContentView.findViewById(R.id.txt_fgt_fgt_detail_3);
        mGsName4 = mContentView.findViewById(R.id.txt_fgt_fgt_detail_4);
        mGsBtn1 = mContentView.findViewById(R.id.downbtn_fgt_game_detail_1);
        mGsBtn2 = mContentView.findViewById(R.id.downbtn_fgt_game_detail_2);
        mGsBtn3 = mContentView.findViewById(R.id.downbtn_fgt_game_detail_3);
        mGsBtn4 = mContentView.findViewById(R.id.downbtn_fgt_game_detail_4);

        mRvImags.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false));
        mRvImags.setNestedScrollingEnabled(false);

    }

    //设置数据；
    public void setData(GameOuterClass.Game game) {
        String[] urls = game.getDetailImage().split("&&");
        List<String> mUrls = new ArrayList<>();
        final HashMap<Integer, String> mUrlsCollection = new HashMap<>();
        for (int i = 0; i < urls.length; i++) {
            mUrls.add(urls[i]);
            mUrlsCollection.put(i, urls[i]);
        }

        if (mGameDesc != null)
            mGameDesc.setText(game.getIntroduce());
        mRecyclerViewAdapter = new RecyclerViewAdapter<String>(getContext(), R.layout.item_image, mUrls) {
            @Override
            public void convert(ViewHolder viewHolder, String url, final int position) {
                View view = viewHolder.getView(R.id.img_detail);
                if (view instanceof ImageView) {
                    Glide.with(FgtGameDetail.this).load(url).into((ImageView) view);
                    view.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent picEnlargeIntent = new Intent(getActivity(), ActPicturesEnlarge.class);
                            picEnlargeIntent.putExtra(KFIntentKeys.EXTRA_CURRENT_PICTURE_INDEX, (position));
                            picEnlargeIntent.putExtra(KFIntentKeys.EXTRA_CURRENT_PICTURE_COLLECTIONS, mUrlsCollection);
                            getActivity().startActivity(picEnlargeIntent);
                        }
                    });
                }

            }
        };
        mRvImags.setAdapter(mRecyclerViewAdapter);


    }
}
