package com.sheep.gamegroup.view.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.AppBarLayout;
import android.view.View;
import com.tencent.smtt.sdk.WebView;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kfzs.duanduan.utils.ApkUtils;
import com.kfzs.duanduan.utils.StatusBarUtils;
import com.sheep.gamegroup.absBase.BaseActivity;
import com.sheep.gamegroup.model.entity.BaseMessage;
import com.sheep.gamegroup.model.entity.NewAboutUs;
import com.sheep.gamegroup.model.util.SheepSubscriber;
import com.sheep.gamegroup.util.GlideImageLoader;
import com.sheep.gamegroup.util.ViewUtil;
import com.sheep.jiuyan.samllsheep.R;
import com.sheep.jiuyan.samllsheep.SheepApp;
import com.sheep.jiuyan.samllsheep.utils.G;
import com.sheep.jiuyan.samllsheep.utils.TitleBarUtils;

import butterknife.BindView;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 关于小绵羊
 * Created by realicing on 2018/6/11.
 */

public class ActNewAboutUs extends BaseActivity {

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        StatusBarUtils.setTranslucent(this);
        super.onCreate(savedInstanceState);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.act_new_about_us;
    }

    @Override
    public void initView() {
        TitleBarUtils.getInstance()
                .setTitle(this,"关于小绵羊")
                .setTitleFinish(this);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, G.BAR_HEIGHT);
        status_height_view.setLayoutParams(params);
        GlideImageLoader.centerImage(about_us_iv, "http://cdngame.kuaifazs.com/icon_phone.png");
        /*
          标题显示和隐藏的监听
         */
        ((AppBarLayout) findViewById(R.id.appbar_act_game_details)).addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (Math.abs(verticalOffset) <
                        appBarLayout.getTotalScrollRange() - G.getRealPix(20)) {
                    if (!mIsExpanded) {
                        mIsExpanded = true;//修改状态标记为展开
                        txt_baseactivity_title.setVisibility(View.INVISIBLE);
                        new_about_us_bar.setVisibility(View.INVISIBLE);
                        title_bottom_line.setVisibility(View.INVISIBLE);
                        about_us_version_tv.setVisibility(View.VISIBLE);
                        img_baseactivity_title.setImageLevel(0);
                    }
                } else if (Math.abs(verticalOffset) >= appBarLayout.getTotalScrollRange()) {
                    if (mIsExpanded) {
                        txt_baseactivity_title.setVisibility(View.VISIBLE);
                        new_about_us_bar.setVisibility(View.VISIBLE);
                        title_bottom_line.setVisibility(View.VISIBLE);
                        about_us_version_tv.setVisibility(View.INVISIBLE);
                        img_baseactivity_title.setImageLevel(1);
                        mIsExpanded = false;//修改状态标记为折叠
                    }
                }
            }
        });
    }
    private boolean mIsExpanded = false;//是否是折叠状态

    @Override
    public void initListener() {

    }
    @Override
    public void initData() {
        showProgress();
        resetData();
        ViewUtil.setText(about_us_version_tv, "版本：V"+ApkUtils.getCurrentPkgVersionName(SheepApp.getInstance()));
        SheepApp.getInstance().getNetComponent().getApiService().getNewAboutUs()
                        .subscribeOn(Schedulers.io())
                        .observeOn(AndroidSchedulers.mainThread())
                        .subscribe(new SheepSubscriber<BaseMessage>(this) {
                            @Override
                            public void onNext(BaseMessage baseMessage) {
                                NewAboutUs newAboutUs = baseMessage.getData(NewAboutUs.class);
                                if(newAboutUs != null){
                                    loadData(newAboutUs);
                                } else {
                                    resetData();
                                }
                                hideProgress();
                            }

                            @Override
                            public void onError(BaseMessage baseMessage) {
                                G.showToast(baseMessage);
                                resetData();
                                hideProgress();
                            }
                        });
    }

    @BindView(R.id.about_us_iv)
    ImageView about_us_iv;
    @BindView(R.id.new_about_us_bar)
    View new_about_us_bar;
    @BindView(R.id.title_bottom_line)
    View title_bottom_line;
    @BindView(R.id.img_baseactivity_title)
    ImageView img_baseactivity_title;
    @BindView(R.id.txt_baseactivity_title)
    View txt_baseactivity_title;
    @BindView(R.id.status_height_view)
    View status_height_view;


    @BindView(R.id.about_us_addr_tv)
    TextView about_us_addr_tv;//相关信息--官方地址
    @BindView(R.id.about_us_platform_tv)
    TextView about_us_platform_tv;//相关信息--官方微信公众平台
    @BindView(R.id.about_us_qq_back_tv)
    TextView about_us_qq_back_tv;//投诉与建议--qq
    @BindView(R.id.about_us_qq_tv)
    TextView about_us_qq_tv;//商务合作--QQ
    @BindView(R.id.about_us_email_tv)
    TextView about_us_email_tv;//商务合作--邮箱
    @BindView(R.id.about_us_other_tv)
    TextView about_us_other_tv;//公司简介--内容
    @BindView(R.id.about_us_other_wv)
    WebView about_us_other_wv;//公司简介--内容
    @BindView(R.id.about_us_version_tv)
    TextView about_us_version_tv;//版本号
    private void loadData(NewAboutUs newAboutUs) {
        ViewUtil.setText(about_us_qq_back_tv, newAboutUs.getComplaintQq());
        ViewUtil.setH5Text(about_us_other_tv, about_us_other_wv, newAboutUs.getContent());
        ViewUtil.setText(about_us_qq_tv, newAboutUs.getCooperationQq());
        ViewUtil.setText(about_us_email_tv, newAboutUs.getMailbox());
        ViewUtil.setText(about_us_addr_tv, newAboutUs.getOfficialAddress());
        ViewUtil.setText(about_us_platform_tv, newAboutUs.getWechatPlatform());
    }
    private void resetData() {
        ViewUtil.setText(about_us_qq_back_tv);
        ViewUtil.setText(about_us_other_tv);
        ViewUtil.setText(about_us_qq_tv);
        ViewUtil.setText(about_us_email_tv);
        ViewUtil.setText(about_us_addr_tv);
        ViewUtil.setText(about_us_platform_tv);
    }

}
