package com.dskj.trustlend;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;

import androidx.annotation.RequiresApi;
import androidx.lifecycle.ViewModelProviders;

import com.azhon.basic.base.BaseActivity;
import com.bumptech.glide.Glide;
import com.dskj.trustlend.api.Api;
import com.dskj.trustlend.api.BaseObserver;
import com.dskj.trustlend.api.Result;
import com.dskj.trustlend.bean.ContactBean;
import com.dskj.trustlend.databinding.ActivityAboutUsBinding;
import com.dskj.trustlend.databinding.ActivityEditUserBinding;
import com.dskj.trustlend.home.MainViewModel;
import com.dskj.trustlend.utils.GsonUtils;
import com.dskj.trustlend.utils.ToastUtils;
import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zhihu.matisse.Matisse;
import com.zhihu.matisse.MimeType;
import com.zhihu.matisse.engine.impl.GlideEngine;
import com.zhihu.matisse.internal.entity.CaptureStrategy;

import java.io.File;
import java.util.List;

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

public class AboutUsActivity extends BaseActivity<MainViewModel, ActivityAboutUsBinding> {
    ContactBean contactBean;
    @Override
    protected int initLayout() {
        return R.layout.activity_about_us;
    }

    @Override
    protected void initView() {
        //setStatusBarColorW(this,R.color.white,false);
        dataBinding.backIv.setOnClickListener(v -> onBackPressed());

        getAbout();

        dataBinding.emailLy.setOnClickListener(v -> toSendEmail());
        dataBinding.telLy.setOnClickListener(v -> toPhone());

    }

    private void toPhone() {
        if(contactBean!=null&&!TextUtils.isEmpty(contactBean.getTel())) {
            copy(contactBean.getTel(),AboutUsActivity.this,"Phone number copied successfully");

            Intent intent = new Intent(Intent.ACTION_DIAL);
            intent.setData(Uri.parse("tel:"+contactBean.getTel())); // 替换1234567890为你想拨打的电话号码
            startActivity(intent);
        }
    }


    /**
     * 实现文本复制功能
     *
     * @param content
     */
    public  void copy(String content, Context context,String toastTips) {
        // 得到剪贴板管理器
        ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
        cmb.setText(content.trim());
        ToastUtils.showShort(dataBinding.backIv,toastTips);
    }

    private void toSendEmail(){
        if(contactBean!=null&&!TextUtils.isEmpty(contactBean.getEmail())) {
            copy(contactBean.getEmail(),AboutUsActivity.this,"Email address copied successfully");

            Intent emailIntent = new Intent(Intent.ACTION_SENDTO);
            emailIntent.setData(Uri.parse("mailto:" +contactBean.getEmail()));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, getString(R.string.app_name));
            emailIntent.putExtra(Intent.EXTRA_TEXT, getString(R.string.app_name));
            try {
                startActivity(Intent.createChooser(emailIntent, "Select Email APP"));
            } catch (android.content.ActivityNotFoundException ex) {
                // 如果设备上没有安装邮箱应用，则显示一个Toast消息
                ToastUtils.showShort(dataBinding.backIv, "No email application installed");
            }
        }

    }

    private void getAbout() {
        Api.getInstance().contact()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<Result<ContactBean>>() {

                    @Override
                    public void onSuccess(Result<ContactBean> feedbackResp) {
                        contactBean = feedbackResp.data;
                       // {"code":1,"msg":"操作成功","data":{"tel":"123456","whatsapp":"456789","email":"123@gmail.com","time":"星期一至星期日-10:00-18:00"}}
                        dataBinding.telsTv.setText(feedbackResp.data.getTel()+"");
                        dataBinding.emailTv.setText(feedbackResp.data.getEmail()+"");
                        dataBinding.aboutUsTv.setText(feedbackResp.data.getTime()+"");


                    }

                    @Override
                    public void onError(int code, String msg) {

                    }
                });
    }


    @Override
    protected void initData() {

    }

    @Override
    protected MainViewModel initViewModel() {
        return ViewModelProviders.of(this).get(MainViewModel.class);
    }

    @Override
    protected void showError(Object obj) {

    }
}