package com.tofly.ghrq_route.common.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.tofly.ghrq_route.R;
import com.tofly.ghrq_route.base.ui.BaseUi;
import com.tofly.ghrq_route.module.checkVersion.domain.CheckReslult;
import com.tofly.ghrq_route.common.util.app.AppUtil;
import com.tofly.ghrq_route.common.util.app.UpdateManager;

/**
 * Created by zombiexq on 2016/6/27.
 */
public class UiAbout extends BaseUi {

    private TextView version_tv;
    private TextView textVersionDesc;
    private String localversion, netversion, url_describe, url_contactus, url_userguide;
    private RelativeLayout versionupdate, versiondescribe, contactus, userguide;
    private UpdateManager updateManager;
    private CheckReslult mCheckReslult;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.ui_about);


        initView();
        initData();
        initListener();

    }

    private void initView() {

        version_tv = (TextView) findViewById(R.id.about_version_tv);
        versionupdate = (RelativeLayout) findViewById(R.id.about_versionupdate);
        versiondescribe = (RelativeLayout) findViewById(R.id.about_versiondescribe);
        contactus = (RelativeLayout) findViewById(R.id.about_contactus);
        userguide = (RelativeLayout) findViewById(R.id.about_userguide);
        textVersionDesc = (TextView) findViewById(R.id.text_version_desc);
    }

    private void initData() {
        updateManager = new UpdateManager(this);

        localversion = AppUtil.getLocalVersionName(UiAbout.this);
        version_tv.setText(localversion);

        int localVersion = AppUtil.getLocalVersionCode(this);
        updateManager.getVersionInfo(localVersion + "", checkReslult -> {
            mCheckReslult = checkReslult;
            String notes = "无内容";
            if (checkReslult != null){
                if (checkReslult.getNotes() != null && !checkReslult.getNotes().isEmpty())
                    notes = checkReslult.getNotes();
            }
            textVersionDesc.setText(notes);
        });
    }

    private AboutListener mListener = new AboutListener();

    private void initListener() {
        versionupdate.setOnClickListener(mListener);
        versiondescribe.setOnClickListener(mListener);
        contactus.setOnClickListener(mListener);
        userguide.setOnClickListener(mListener);
    }

    public class AboutListener implements View.OnClickListener {

        @Override
        public void onClick(View v) {
            int id = v.getId();
            switch (id) {
                case R.id.about_versionupdate://检查版本
                    updateManager.checkUpdate();
                    break;
                case R.id.about_versiondescribe://检查版本
                    turnToWeb("版本介绍", url_describe);
                    break;
                case R.id.about_contactus:
                    turnToWeb("联系我们", url_contactus);
                    break;
                case R.id.about_userguide:
                    turnToWeb("用户指南", url_userguide);
                    break;
            }
        }
    }

    //跳转到UiWeb
    private void turnToWeb(String title, String url_describe) {
        Intent intent = new Intent();
        intent.setClass(UiAbout.this, UiWeb.class);
        Bundle bundle = new Bundle();
        bundle.putString("title", title);
        bundle.putString("url", url_describe);
        intent.putExtras(bundle);
        startActivity(intent);
    }
}
