package com.dengjian.soccermap.ui.fragment;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.dengjian.soccermap.R;
import com.dengjian.soccermap.adapter.RaceDataAdapter;
import com.dengjian.soccermap.adapter.RaceSchedulesAdapter;
import com.dengjian.soccermap.model.EventConstants;
import com.dengjian.soccermap.model.PreferenceConstants;
import com.dengjian.soccermap.model.StatisticsConstants;
import com.dengjian.soccermap.thirdparty.pulltorefresh.PullToRefreshBase;
import com.dengjian.soccermap.thirdparty.pulltorefresh.PullToRefreshListView;
import com.dengjian.soccermap.ui.activity.MainActivity;
import com.dengjian.soccermap.utils.PreferenceHelper;
import com.umeng.analytics.MobclickAgent;

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

/**
 * Created by dengjian697 on 17/3/7.
 */
public class DataFragment extends ContentFragment {
    public static final String TAG = DataFragment.class.getSimpleName();

    private ViewGroup mViewGroup;
    private Dialog mExitDialog;
    private LinearLayout mLlNoData;
    private TextView mTvNoData;

    private PullToRefreshListView mLvData;

    @Override
    protected void initView() {

    }

    @Override
    protected View onCreateContentView(LayoutInflater inflater) {
        mViewGroup = (ViewGroup) inflater.inflate(R.layout.frag_data, null);
        mLlNoData = (LinearLayout) mViewGroup.findViewById(R.id.ll_no_data);
        mTvNoData = (TextView) mViewGroup.findViewById(R.id.tv_no_data);
        mLvData = (PullToRefreshListView) mViewGroup.findViewById(R.id.lv_data);
        ListView refreshableDataListView = mLvData.getRefreshableView();
        mLvData.setMode(PullToRefreshBase.Mode.PULL_FROM_START);
        mLvData.setPullLabel(mContext.getString(R.string.pull_to_refresh_pull_label_racedata));
        mLvData.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                MobclickAgent.onEvent(mContext, StatisticsConstants.STAT_PULL_TO_REFRESH);
                String raceTitleType = PreferenceHelper.getInstance(mContext).getString(
                        PreferenceConstants.KEY_RACE_TITLE_TYPE, "");
                if (getString(R.string.soccer_race_type_china).equals(raceTitleType)) {
                    mActivity.qryRaceData();
                } else if (getString(R.string.soccer_race_type_epl).equals(raceTitleType)) {
                    mActivity.qryRaceData(false, getString(R.string.soccer_race_type_epl));
                } else if (getString(R.string.customize_race_name).equals(raceTitleType)) {
                    if (TextUtils.isEmpty(PreferenceHelper.getInstance(mContext).getString(
                            PreferenceConstants.KEY_CUSTOMIZE_RACE_JSON, null))) {
                        // 未设置过自定义赛程，直接进入自定义赛程页面l
                        mActivity.getUtilFragmentManager().showFragment(
                                UtilFragmentManager.TYPE_CUSTOMIZE_RACE, null, false);
                    } else {
                        mActivity.qryRaceData(true);
                    }
                } else {
                    mActivity.qryRaceData();
                }
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {

            }
        });
//        mLvData.getLoadingLayoutProxy().setLoadingDrawable(
//                mContext.getResources().getDrawable(R.drawable.loading_progressbar_top_style));
        if (null == mBaseAdapter) {
            mBaseAdapter = new RaceDataAdapter(mContext);
        }
        mLvData.setAdapter(mBaseAdapter);
        if (null != refreshableDataListView) {
            refreshableDataListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                @Override
                public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                }
            });
        }

        mBHomeTabFragment = true;
        EventBus.getDefault().register(this);

        if (null != mLlNoData && null != mTvNoData) {
            if (MainActivity.QryStatus.QRY_TYPE_ONLINE_FAILED.equals(mActivity.mQryStatus)) {
                mLlNoData.setVisibility(View.VISIBLE);
                mTvNoData.setText(getString(R.string.ranking_data_qry_failed));
            } else if (MainActivity.QryStatus.QRY_TYPE_ONLINE_SUCCESSED.equals(mActivity.mQryStatus)) {
                mLlNoData.setVisibility(View.GONE);
            } else {    // 本地不支持
                mLlNoData.setVisibility(View.VISIBLE);
                mTvNoData.setText(getString(R.string.ranking_no_data));
            }
        }

        return mViewGroup;
    }

    @Override
    protected void onUpdateOrientation(int orientation) {
        mOrientation = getResources().getConfiguration().orientation;
    }

    @Override
    public boolean onBackPressed() {
        if (null != mActivity) {
            mActivity.quitSoccerMap();
        }
        return true;
    }

    private void openExitAppDialog() {
        mExitDialog = new AlertDialog.Builder(mActivity)
                .setTitle(R.string.tip_title)
                .setMessage(R.string.dg_exit_message)
                .setPositiveButton(R.string.btn_yes, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        mActivity.finish();
                    }
                }).setNegativeButton(R.string.btn_no, new DialogInterface.OnClickListener() {

                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                }).create();
        mExitDialog.show();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (null != mActivity) {
            mActivity.setHomeTitleName(getString(R.string.tab_data));
        }
        // 统计
        MobclickAgent.onPageStart(TAG);
    }

    @Override
    public void onPause() {
        super.onPause();
        // 统计
        MobclickAgent.onPageEnd(TAG);
    }

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

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceiveMsgEvent(Intent intent) {
        if (null != intent) {
            String action = intent.getAction();
            if (EventConstants.ACTION_PULL_TO_REFRESH_COMPLETE.equals(action)) {    // 也表示请求完成
                mLvData.onRefreshComplete();
                if (null != mLlNoData && null != mTvNoData) {
                    if (MainActivity.QryStatus.QRY_TYPE_ONLINE_FAILED.equals(mActivity.mQryStatus)) {
                        mLlNoData.setVisibility(View.VISIBLE);
                        mTvNoData.setText(getString(R.string.ranking_data_qry_failed));
                    } else if (MainActivity.QryStatus.QRY_TYPE_ONLINE_SUCCESSED.equals(mActivity.mQryStatus)) {
                        mLlNoData.setVisibility(View.GONE);
                    } else {    // 本地不支持
                        mLlNoData.setVisibility(View.VISIBLE);
                        mTvNoData.setText(getString(R.string.ranking_no_data));
                    }
                }
            }
        }
    }
}
