package com.itant.joycity.fragment.first;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;

import com.itant.joycity.R;
import com.itant.joycity.api.JoyUrl;
import com.itant.joycity.fragment.BaseFragment;
import com.itant.joycity.tool.AppTool;
import com.itant.joycity.tool.StringTool;
import com.itant.joycity.tool.ToastTool;

import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Jason on 2016/10/6.
 */
public class VideoFragment extends BaseFragment {

    //private Button btn_you_hui;
    private Button btn_video;
    private String youHuiUrl;// 优惠链接地址
    private String videoUrl;// 视频链接地址

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_video, null);

        btn_video = (Button) view.findViewById(R.id.btn_video);
        /*btn_you_hui = (Button) view.findViewById(R.id.btn_you_hui);

        btn_you_hui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(youHuiUrl)) {
                    // 跳转到网址
                    AppTool.openUrlWithBrowser(getActivity(), youHuiUrl);
                } else {
                    ToastTool.showShort(getActivity(), "未获取到有效链接");
                }
            }
        });*/


        btn_video.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(videoUrl)) {
                    // 跳转到网址
                    AppTool.openUrlWithBrowser(getActivity(), videoUrl);
                } else {
                    ToastTool.showShort(getActivity(), "未获取到有效链接");
                }
            }
        });

        getVideoUrl();

        return view;
    }

    /**
     * 获取视频链接
     */
    public void getVideoUrl() {
        RequestParams params = new RequestParams(JoyUrl.URL_VIDEO);
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                if (!TextUtils.isEmpty(result)) {
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        JSONObject obj = StringTool.getUsefulObjectFromArray(jsonObject, "Video");
                        if (obj != null) {
                            try {
                                videoUrl = obj.getString("Url").trim();
                                //youHuiUrl = obj.getString("Url").trim();
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                } else {
                    ToastTool.showShort(getActivity(), "获取数据失败");
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                //Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                //Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {

            }
        });
    }
}
