package com.dinect.checker;

import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.Toast;
import android.util.Log;
import com.dinect.checker.zbar.CameraActivity;
import com.dinect.checker.zxing.ScannerActivity;
import io.flutter.app.FlutterActivity;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.Result;
import io.flutter.plugins.GeneratedPluginRegistrant;
import android.support.v4.app.Fragment;
import android.Manifest;
import android.content.pm.PackageManager;
import android.support.v4.content.ContextCompat;
import android.support.v4.app.ActivityCompat;
import android.content.pm.PackageInfo;

import java.util.*;

public class MainActivity extends FlutterActivity {

    private static final int START_SCANNER_REQUEST_CODE = 2017;
    private static final String FLUTTER_CHANNEL_NAME = "com.dinect.checker/instance_id";

    static final String PREF_API_URL = "url";
    static final String PREF_APP_TOKEN = "appToken";
    static final String PREF_POS_TOKEN = "token";
    static final String PREF_APP_BAR_COLOR = "color";

    static final String SCANNER_BACKEND_KEY = "scanner_backend_idx";

    static final int ZXING = 0;
    static final int ZBAR = 1;

    static final Class[] SCANNER_BACKEND = {
            ScannerActivity.class,
            CameraActivity.class,
    };

    private MethodChannel mChannel;
    private Map mScannerArgs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        GeneratedPluginRegistrant.registerWith(this);
        mChannel = new MethodChannel(getFlutterView(), FLUTTER_CHANNEL_NAME);
        mChannel.setMethodCallHandler(
                new MethodChannel.MethodCallHandler() {
                    @Override
                    public void onMethodCall(MethodCall call, Result result) {
                        callMethod(call, result);
                    }
                });
    }

    private void callMethod(MethodCall call, Result result) {
        switch (call.method) {
            case "getLocale":
                result.success(getLanguage());
                break;
            case "getFlavor":
                result.success(BuildConfig.flavor);
                break;
            case "getCurrency":
                result.success(BuildConfig.currency);
                break;
            case "startScanner":
                mScannerArgs = call.arguments();
                startScannerActivity();
                break;
            case "isOnline":
                checkInternetConnection(result);
                break;
            case "getSupportPhone":
                result.success(BuildConfig.supportPhone);
                break;
            case "getSupportUrl":
                result.success(BuildConfig.supportUrl);
                break;
            case "getEndpoint":
                result.success(BuildConfig.endpoint);
                break;
            case "getAppToken":
                result.success(BuildConfig.appToken);
                break;
            case "getAppTitle":
                result.success(BuildConfig.appTitle);
                break;
            case "showBonus":
                result.success(BuildConfig.showBonus);
                break;
            case "finish":
                Log.d("kifio", call.method);
                finish();
                break;
            case "getVersionName":
                result.success(getVersion());
                break;
            default:
                result.notImplemented();
                break;
        }
    }

    private String getVersion() {
      try {
        PackageInfo pInfo = this.getPackageManager().getPackageInfo(getPackageName(), 0);
        return pInfo.versionName;
      } catch (PackageManager.NameNotFoundException e) {
        e.printStackTrace();
        return "";
      }
    }

    private String getLanguage() {
        List<String> availableLanguages = Arrays.asList("ru", "en");
        if (availableLanguages.contains(Locale.getDefault().getLanguage())) {
            return Locale.getDefault().getLanguage();
        } else {
            return BuildConfig.locale;
        }
    }

    private void checkInternetConnection(Result result) {
        boolean connected = Utils.isOnline(this);
        if (!connected)
            Toast.makeText(this, "Проверьте интернет соединение", Toast.LENGTH_SHORT).show();
        result.success(connected);
    }

    private void setLocale(String locale) {
        locale = locale != null ? locale : getLanguage();
        Resources res = getResources();
        Configuration configuration = new Configuration(res.getConfiguration());
        configuration.locale = new Locale(locale);
        Locale.setDefault(configuration.locale);
        res.updateConfiguration(configuration, res.getDisplayMetrics());
    }

    private void startScannerActivity() {
      if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 101);
      } else {
        final int idx = getSharedPreferences("scanner", Context.MODE_PRIVATE).getInt(
          SCANNER_BACKEND_KEY, 0);
        Intent cameraIntent = new Intent(MainActivity.this, SCANNER_BACKEND[idx]);

        for (Object key : mScannerArgs.keySet()) {
          if (key.equals("color")) {
            cameraIntent.putExtra((String) key, Long.parseLong((String) mScannerArgs.get(key)));
          } else {
            cameraIntent.putExtra((String) key, (String) mScannerArgs.get(key));
          }
        }

        setLocale((String) mScannerArgs.get("localeCode"));
        startActivityForResult(cameraIntent, START_SCANNER_REQUEST_CODE);
      }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode,
      String permissions[], int[] grantResults) {
      if (requestCode == 101) {
        if (grantResults.length > 0
          && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
          startScannerActivity();
        }
      }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == START_SCANNER_REQUEST_CODE) {
            if (resultCode == RESULT_CANCELED) {
                finish();
            } else if (resultCode == RESULT_OK) {
                if (data != null) {
                    String user = data.getExtras().getString("user", null);
                    if (user != null) {
                        String card = data.getExtras().getString("card", null);
                        ArrayList<String> args = new ArrayList<>(2);
                        args.add(user);
                        args.add(card);
                        mChannel.invokeMethod("purchase", args);
                    } else {
                        String menuItem = data.getExtras().getString("item", null);
                        if (menuItem != null) {
                          if (menuItem.equals("exit")) {
                            finish();
                          } else {
                            mChannel.invokeMethod(menuItem, null);
                          }
                        }
                    }
                } else {
                    startScannerActivity();
                }
            }
        }
    }

    public void getFlavor() {

    }

    public void getCurrency() {

    }

    public void getLocale() {

    }

    public void setUserLocale() {

    }

    public void startScanner() {

    }

    public void isOnline() {

    }

    public void getSupportPhone() {

    }

    public void getSupportUrl() {

    }

    public void getEndpoint() {

    }

    public void getAppToken() {

    }

    public void getVersionName() {

    }

    public void setStrings() {

    }
}
