package com.rv.travel;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.base.utils.base.BaseActivity;
import com.frame.rv.OnModuleEventListener;
import com.rv.travel.api.ApiUtil;
import com.rv.travel.api.OnApiResponseCallback;
import com.rv.travel.bean.BeanHot;
import com.rv.travel.bean.BeanTravelMember;
import com.rv.travel.fragment.TravelFragmentHotType;
import com.rv.travel.fragment.TravelFragmentMain;
import com.yuyife.okgo.OkGoUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TravelActivity extends BaseActivity {
    public static boolean isNeedLogin = false;  //是否需要登录
    public static boolean isGoHotType = false;  //是否需要显示热门游碎片
    public static BeanHot goHotTypeData = null;//需要显示热门游碎片,要对其赋值
    @SuppressLint("StaticFieldLeak")
    private static TravelActivity instance;

    public static TravelActivity getInstance() {
        return instance;
    }

    public void setHotTypeTitle(String name) {
        typeText.setText(name);
    }

    @BindView(R2.id.travel_city_layout)
    LinearLayout cityLayout;
    @BindView(R2.id.travel_type_text)
    TextView typeText;
    @BindView(R2.id.travel_city_text)
    TextView cityText;
    @BindView(R2.id.search_input)
    EditText searchInput;
    @BindView(R2.id.search_commit)
    TextView searchCommit;
    @BindView(R2.id.travel_server_image)
    ImageView serverImage;
    @BindView(R2.id.travel_hot_more_layout)
    LinearLayout hotMoreTitleLayout;
    @BindView(R2.id.travel_hot_type_layout)
    FrameLayout hotTypeTitleLayout;


    @OnClick({
            R2.id.travel_hot_type_back,
            R2.id.travel_hot_type_select,
            R2.id.search_commit,
            R2.id.travel_server_image,
    })
    public void onTravelActivityClick(View view) {
        int id = view.getId();
        if (id == R.id.travel_hot_type_back) {
            setTravelFragmentMain();
        } else if (id == R.id.travel_hot_type_select) {
            travelFragmentHotType.setHotMoreFragment();
        } else if (id == R.id.search_commit) {
            String query = searchInput.getText().toString();
            if (travelFragmentMain != null) {
                travelFragmentMain.getRimData(query);
            }
        } else if (id == R.id.travel_server_image) {
            showToastShort("联系客服");
        }
    }

    @SuppressLint("StaticFieldLeak")
    public static LinearLayout hotMoreTitleLayoutStatic;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_travel);
        ButterKnife.bind(this);
        instance = this;
//        ModuleEventUtil.currentActivity = this;
        hotMoreTitleLayoutStatic = hotMoreTitleLayout;
        onData();
        onView();
    }

    @Override
    public void onData() {
//        tabLayout.setCurrent(OnModuleEventListener.MODULE_TRAVEL);
        //13766666666 12345678

        //13612688539  12345678
//        ApiUtil.getToken("13612688539", "12345678", new OnApiResponseCallback<Boolean, String>() {
//            @Override
//            public void done(Boolean data, String errorMessage) {
//                if (data) {
//
//                }
//            }
//        });

        //判断Token是否过期
        ApiUtil.getTravelMember("1", new OnApiResponseCallback<List<BeanTravelMember>, String>() {
            @Override
            public void done(List<BeanTravelMember> data, String errorMessage) {
                if (errorMessage != null && data == null) {
                    OkGoUtil.setToken(null);
                }
            }
        });


    }

    @Override
    public void onView() {
        if (TravelActivity.isGoHotType) {
            if (goHotTypeData != null) {
                setTravelFragmentHotType(goHotTypeData);
            } else {
                showToastShort("从其他模块进入热门游，未携带数据");
            }
        } else {
            setTravelFragmentMain();
        }

        searchInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s != null && s.length() > 0) {
                    searchCommit.setVisibility(View.VISIBLE);
                } else {
                    searchCommit.setVisibility(View.GONE);
                    if (travelFragmentMain != null) {
                        travelFragmentMain.getRimData("");
                    }
                }
            }
        });
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
//        ModuleEventUtil.currentActivity = this;

    }

    private TravelFragmentMain travelFragmentMain;

    //主页面，点击 热门游 更多 弹出列表  
    public void setTravelFragmentMain() {
        hotMoreTitleLayout.setVisibility(View.GONE);
        hotTypeTitleLayout.setVisibility(View.GONE);
        if (travelFragmentMain == null) {
            travelFragmentMain = new TravelFragmentMain();
        }
        setFragment(travelFragmentMain);
    }

    private TravelFragmentHotType travelFragmentHotType;

    //点击 热门游 弹出列表  点击列表条目，设置
    public void setTravelFragmentHotType(BeanHot bean) {
        hotMoreTitleLayout.setVisibility(View.GONE);
        hotTypeTitleLayout.setVisibility(View.VISIBLE);
        travelFragmentHotType = TravelFragmentHotType.getInstance(bean);
        setFragment(travelFragmentHotType);
    }

    private void setFragment(Fragment fragment) {

        getSupportFragmentManager().beginTransaction().replace(R.id.travel_fragment_layout, fragment).commitAllowingStateLoss();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        isGoHotType = false;
        goHotTypeData = null;
        instance = null;
        hotMoreTitleLayoutStatic = null;
//        if (ModuleEventUtil.getOnModuleEventListener() != null) {
//            ModuleEventUtil.getOnModuleEventListener().onClear();
//        }
    }
}
