package com.mjsheng.myappstore.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.mjsheng.myappstore.R;
import com.mjsheng.myappstore.adapter.GaringRecommedAdapter;
import com.mjsheng.myappstore.bean.AppDateInfo;
import com.mjsheng.myappstore.bean.JsonCallback;
import com.mjsheng.myappstore.bean.LzyResponse;
import com.mjsheng.myappstore.callback.GalleryItemClickAnimationListener;
import com.mjsheng.myappstore.callback.GalleryItemClickListener;
import com.mjsheng.myappstore.dialog.AppDetailDialog;
import com.mjsheng.myappstore.utils.Configure;
import com.mjsheng.myappstore.utils.ScaleAnimEffect;
import com.mjsheng.myappstore.utils.Utils;
import com.mjsheng.myappstore.view.GaringRecommedLayout;
import com.mjsheng.myappstore.widget.Sound;

import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by chris on 2015/12/9.
 */
public class AppRecommendFragment extends Fragment {
    private static float big_x = 1.08F;
    private static float big_y = 1.08F;
    private GaringRecommedLayout mGaringRecommedLayout = null;
    private GaringRecommedAdapter mGaringRecAdapter = null;
    private ScaleAnimEffect mAnimEffect;
    private Sound sound;
    private Handler mHandler = new Handler() {

        @SuppressWarnings("unchecked")
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            Bundle data = msg.getData();
            switch (msg.what) {
                case Configure.SUCCESS:
                    if (data != null) {
                        String httTag = data.getString("httpTag");
                        if (TextUtils.isEmpty(httTag)) return;
                        if (null == msg.obj) return;
                        if (httTag.equals(Configure.HTTP_TAG_APP_RECOMMEND)) {
                            List<AppDateInfo> infos = (List<AppDateInfo>) msg.obj;
                            if (mGaringRecAdapter != null) {
                                mGaringRecAdapter.setData(infos);
                                mGaringRecAdapter.notifyDataSetChanged();
                                mGaringRecommedLayout.setAdapter(mGaringRecAdapter);
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }

    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_app_recommend, container, false);
        sound = new Sound(getContext());
        mGaringRecommedLayout = (GaringRecommedLayout) v.findViewById(R.id.recommend_ll);
        mGaringRecommedLayout.setImageGalleryItemClickListener(new GaringRecommItemClickListener());
        mGaringRecAdapter = new GaringRecommedAdapter(getActivity());

        return v;
    }

    public void initDatas(String appId) {
        OkGo.get(Configure.HTTP_TAG_APPDETAIL_URL + appId)
                .tag(this)
                .cacheKey(Configure.HTTP_TAG_APPDETAIL_URL + appId)
                .cacheMode(CacheMode.REQUEST_FAILED_READ_CACHE)
                .execute(new JsonCallback<LzyResponse<List<AppDateInfo>>>() {
                    @Override
                    public void onSuccess(LzyResponse<List<AppDateInfo>> listLzyResponse, Call call, Response response) {
                        Message msg = new Message();
                        msg.what = Configure.SUCCESS;
                        Bundle bundle = new Bundle();
                        bundle.putString("httpTag", Configure.HTTP_TAG_APP_RECOMMEND);
                        msg.obj = listLzyResponse.data;
                        msg.setData(bundle);
                        mHandler.sendMessage(msg);
                    }
                    @Override
                    public void onCacheError(Call call, Exception e) {
                        super.onCacheError(call, e);
                        Utils.showToast(getContext(), "网络连接不稳定，请稍后重试！");
                    }
                });
    }



    private AppDetailDialog appDetailDialog;
    public void setParentView(AppDetailDialog dialog) {
        this.appDetailDialog = dialog;
    }

    public void getAppDetail(String appBaoming, String s) {
        appDetailDialog.finish();
        Intent intent = new Intent(getActivity(), AppDetailDialog.class);
        intent.putExtra("title","推荐");
        intent.putExtra("appBaoming",appBaoming);
        startActivity(intent);
    }

    private class GaringRecommItemClickListener implements GalleryItemClickListener {

        @Override
        public void onGalleryItemClick(final View view, int position) {
            sound.playSoundEffect();
            if (view != null) {
                mAnimEffect = ScaleAnimEffect.getInstance();
                mAnimEffect.setAttributs(1.0F, big_x, 1.0F, big_y, 200);
                Animation anim = mAnimEffect.createAnimation();
                anim.setFillBefore(true);
                mAnimEffect.startAnimation(anim, view, new GalleryItemClickAnimationListener() {
                    @Override
                    public void onAnimationEnd() {
                        if (view != null) {
                            AppDateInfo appInfo = (AppDateInfo) view.getTag();
                            if (appInfo != null) {
                                getAppDetail(appInfo.getApp_baoming(), null);
                            }
                        }
                    }
                });
            }
        }
    }


}
